ef6: Update-Database fails creating a database with mssqllocaldb

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

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 15
  • Comments: 24 (7 by maintainers)

Most upvoted comments

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

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)

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

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:\Users\nemkeaspnet-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

Fixed in latest SQL Server 2017 CU!

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?!

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 ‘\’

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.

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 problem when running Update-Database i got this messag, am running a SQL server 2017 any help guys?

@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

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