Delete .mdf file from app_data causes exception cannot attach the file as database

asp.net-mvc ef-code-first entity-framework

Question

I am building a web application using VS 2012 MVC4 code first. In order to recreate the .mdf file after I changed the model, I manually deleted the file from the app_data directory in VS. I have done this a few times before without any problem.

Now I receive an exception:

The underlying provider failed on Open. ==> Cannot attach the file MYDB.mdf as database 'MYDB'.

I will appreciate your help on how to recreate the .mdf file.

Thank you!

1
49
10/21/2012 8:30:59 PM

Accepted Answer

Use SQL Server Object Explorer to delete the database rather than just deleting the .mdf file in Solution Explorer. Otherwise the SQL Server instance still thinks the database name is used and the next time you try to attach a database to the same name it won't work. See the section on Initial Catalog in this MSDN page:

http://msdn.microsoft.com/en-us/library/jj653752.aspx#initialcatalog

31
10/22/2012 4:01:26 PM

Popular Answer

That what fixed it for me, From Package Manager Console run these commands:

  sqllocaldb.exe stop v11.0
  sqllocaldb.exe delete v11.0

And then:

  Update-Database


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow