Ef6: Update-Database fails creating a database with mssqllocaldb

Created on 22 Oct 2017  ·  23Comments  ·  Source: dotnet/ef6

Dear Maintainer,

Update-Database fails creating a database with mssqllocaldb when using the default App.config
I receive the following error:
CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file 'C:\Users\[Username][Projectname].[Contextclass].mdf'.

It looks like it is missing a '\' between the username and the projectname.

Is this database location assigned by SQL Server LocalDb or is this a problem in 'System.Data.Entity.Infrastructure.LocalDbConnectionFactory'?

Thank you in advance.

Best regards,

NiekGr

closed-external

Most helpful comment

Seems related to conflicting version of SQLLocalDB.
Visual Studio comes with version 2016. The problem occurs when you have installed version 2017.
To resolve the problem follow these steps:

  • Uninstall Microsoft SQL SERVER 2017 LocalDB
  • Open command prompt
  • SqllocalDB i "MSSQLLocalDB" (will probably give you an error)
  • SqllocalDB delete "MSSQLLocalDB"
  • SqllocalDB create "MSSQLLocalDB"
    This will recreate the instance using the correct (2016) version of SQL Server LocabDB

EntityFrameWork migraton will now succeed (slash is added to the path)

All 23 comments

I got the same issue, but with EF Core, and only with localdb (Regular SQL Server Express works fine)

CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file 'C:\Usersnemkeaspnet-WebApplication1-083FBAB5-A996-467F-8312-ACEEC24C3A0C.mdf'.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

My windows username nemke is concatenated with database name _nemkeaspnet-WebApplication1-083FBAB5-A996-467F-8312-ACEEC24C3A0C.mdf_

It seems like this is a problem with localdb v14
https://dba.stackexchange.com/questions/191393/localdb-v14-creates-wrong-path-for-mdf-files
http://answers.flyppdevportal.com/MVC/Post/Thread/f332e6e4-ed6d-4a9e-b8a6-a04bdfd64630?category=sqldatabaseengine

Same problem:

Error Number:5123,State:1,Class:16
CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or
create the physical file 'C:\Users\samMicrosoftOAuthExample.mdf'.

filename should be: 'C:\Users\sam\MicrosoftOAuthExample.mdf'
Just noticed this the EF6 forum.... I am using Microsoft.EntityFrameworkCore.Tools 2.0.1

We had the same issue. Created the database manually and got Update-Database initialize it. That resolved our problem but the root cause must be addressed anyway.

Same here guys, will try creating the database manually as a workaround.

See https://github.com/aspnet/EntityFrameworkCore/issues/10288#issuecomment-372509820 for more details on the investigation. In summary:

  • This is due to a breaking change in SQL Server 2017
  • One of the workarounds is to set the full path using AttachDbFileName in the connection string

Today, I confirmed that you don't even need EF to repro this issue, and we have reported this to the SQL Server team. The saving grace of this is that Visual Studio 2017 still installs the SQL Server 2016 version of LocalDB.

Given all this, at this point we don't plan to make any code changes in EF Core or EF6 to try to compensate for the SQL Server bug.

Here are repro steps without EF:

  1. Install SQL Server 2017 LocalDB
  2. Open Microsoft SQL Server Management Studio
  3. Connect it to any SQL Server 2017 LocalDB
  4. Open a new query window
  5. Execute “CREATE DATABASE Hello”

Expected result:

The database Hello is created.

Actual result:

Msg 5123, Level 16, State 1, Line 1
CREATE FILE encountered operating system error 5(Access is denied.) while attempting to open or create the physical file 'C:\Users\MyUsernameHello.mdf'.
Msg 1802, Level 16, State 4, Line 1
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.

Thank you

Seems related to conflicting version of SQLLocalDB.
Visual Studio comes with version 2016. The problem occurs when you have installed version 2017.
To resolve the problem follow these steps:

  • Uninstall Microsoft SQL SERVER 2017 LocalDB
  • Open command prompt
  • SqllocalDB i "MSSQLLocalDB" (will probably give you an error)
  • SqllocalDB delete "MSSQLLocalDB"
  • SqllocalDB create "MSSQLLocalDB"
    This will recreate the instance using the correct (2016) version of SQL Server LocabDB

EntityFrameWork migraton will now succeed (slash is added to the path)

Fixed in latest SQL Server 2017 CU!

@ErikEJ what's the best way to fix this problem? I'm still having customers hit it. See https://github.com/aspnet/Docs/issues/7599

Installing and applying the latest SQL Server 2017 CU is probably the simplest cure.

Applied last SQL Server 2017 CU but this doesn't fix the problem:
"CREATE FILE encountered operating system error 5(Toegang geweigerd.) while attempting to open or create the physical file 'C:\Users[username]MvcMovieContext-94a57038-3170-497a-b0f0-b7cf52e78fec.mdf'.
CREATE DATABASE failed. Some file names listed could not be created. Check related errors."

Username is still missing '\'

@allthefunnamesaretaken

SELECT @@version

On your localdb instance, please

Microsoft SQL Server 2017 (RTM) - 14.0.1000.169 (X64) Aug 22 2017 17:04:49 Copyright (C) 2017 Microsoft Corporation Express Edition (64-bit) on Windows 10 Pro 10.0 (Build 17134: ) (Hypervisor)

Obviously, you did not manage to apply the latest CU to your Localdb instance. Latest version is 14.0.3030

You are right. I didn't notice my update failed.
Update now succeeded and also the EF migration

still having the issue even after updating to
Microsoft SQL Server 2017 (RTM-CU10) (KB4342123) - 14.0.3037.1 (X64) Jul 27 2018 09:40:27 Copyright (C) 2017 Microsoft Corporation Express Edition (64-bit) on Windows 10 Home 10.0 (Build 10586: )
how do i resolve this? I'm using VS2017

I'm also still running into this issue, even with LocalDB version 14.0.3045.24.
I had to add AttachDbFileName as suggested by @divega

Installing and applying the latest SQL Server 2017 CU is probably the simplest cure.

@ErikEJ
But it is more than 800 MB, the total LocalDB 2017 is just below 50 MB. Is there a better way to fix such a tiny bug from microsoft?!

@saeed-serpooshan Not that I know of

Surprised to see this bug is still there as of now, using localdb.

same problem any detail tu fix this problem when running Update-Database i got this message

same problem when running Update-Database i got this messag, am running a SQL server 2017 any help guys?

Was this page helpful?
0 / 5 - 0 ratings