Basically I\'ve followed a tutorial and decided to delete the .mdf
file afterwards.
Now whenever I try to run the application I get the following error
I found that commenting out the context section used to initialise the database resolved the problem. Havnt had time to find out what was wrong with the seeding statements yet, but removing the seeding resolved the problem.
We just ran into this ourselves when running dotnet ef database update
using ASP.Net Core 2.1; looks like relative paths aren't supported with AttachDbFileName
.
System.Data.SqlClient.SqlException (0x80131904): Cannot attach the file '.\OurDbName.mdf' as database 'OurDbName'.
Just wanted to express that here for other people who might be bumping into this. The "fix" is use absolute paths.
See also: https://github.com/aspnet/EntityFrameworkCore/pull/6446
In my case removing Initail Cataloge=....
from connection string resolved the issue
I had the same error. Weird thing was, I had one new project form scratch, there it worked perfectly and another, much bigger project, where I always ran into that error message.
The perfecrtly working project (nearly) always creates the database (indluding the files) automatically. It can be any command, read, write, update. The files get created. Of course it uses
DropCreateDatabaseIfModelChanges
There is only one case, when it gets troubled: IF the mdf is auto-created and you delete the mdf and log file. Then that was about it. Say good-bye to your autocreation...
The only way I found to fix it was like mentioned:
sqllocaldb stop v11.0 & sqllocaldb delete v11.0
After that, everything is back to normal (and all other Databases handled by LocalDB also gone!).
EDIT: That was not true. I just tried it and the v11.0 gets automatically recreated and all mdfs stay available. I have not tried with "non file based" LocalDBs.
The confusing thing is, I also got this error if something else was wrong. So my suggestion is, if you want to make sure your DB-Setup is sound and solid: Create a new solution/project from scratch, use the most basic DB commands (Add an entity, show all entities, delete all entities) and see if it works.
If YES, the problem is somewhere in the abyss of VS2013 config and versioning and nuget and stuff.
IF NO, you have a problem with your LocalDB installation.
For anyone who really wants to understand what's going on in EF (and I am still not sure if I do :-) ) http://odetocode.com/Blogs/scott/archive/2012/08/14/a-troubleshooting-guide-for-entity-framework-connections-amp-migrations.aspx
P.S.: Nudge me if you need the running example project.
Just change database name from web.config project level file and then update database.
connectionString = Data Source =(LocalDb)\MSSQLLocalDB;AttachDbFilename="|DataDirectory|\aspnet-Project name-20180413070506.mdf";Initial Catalog="aspnet--20180413070506";Integrated
Change the bold digit to some other number:
connectionString = Data Source==(LocalDb)\MSSQLLocalDB;AttachDbFilename="|DataDirectory|\aspnet-Project name-20180413070507.mdf";Initial Catalog="aspnet--20180413070507";Integrated
I had the same error while following the tutorial on "Getting Started with ASP.NET MVC 5 | Microsoft Docs". I was on Visual Studio 2015. I opened View-> SQL Server Object Explorer and deleted the database named after the tutorial, then it could work. see Delete .mdf file from app_data causes exception cannot attach the file as database