49

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!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Shlo
  • 1,036
  • 2
  • 10
  • 23

6 Answers6

101

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
Gleb Esman
  • 1,221
  • 2
  • 10
  • 7
  • 6
    If you want to see what your LocalDb database instance is called, run `sqllocaldb info` (in the answer above the instance is called `v11.0` which is the vs2012 ef5 default). – Bakanekobrain Dec 24 '13 at 07:38
  • 3
    only needed to do the first two steps (stop and delete) to resolve my issue – JonAlb May 24 '14 at 19:46
  • 1
    This is the only thing I've found that resolved the issue. I tried renaming the AttachDBFileName and InitialCatalog and neither worked. – matt. Jul 12 '14 at 03:24
  • Since I had already deleted the database the accepted answer was not a good solution. This took care of the issue for me so a new database will be created. I will use the accepted answer to delete the database in the future, or perhaps just run these commands. – Brettski Sep 25 '14 at 00:06
  • I also had to do "sqllocaldb create v11.0" before I could do "update-database". – mdc Mar 16 '15 at 21:25
  • I ran the commands in Package MAnager Console in following order to get it working. `sqllocaldb.exe stop v11.0` `sqllocaldb.exe delete v11.0` `sqllocaldb.exe start v11.0` And then `update-database` – satish suthar Jun 29 '16 at 05:25
  • Also try "Enable-Migrations" and "Add-Migration Init" before update-database – robor Aug 07 '16 at 11:25
31

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

tdykstra
  • 5,880
  • 2
  • 23
  • 20
  • 1
    I guess you are right, since i did delete the .mdf file directly from the solution explorer. I also read about what you are suggesting here: [link](http://odetocode.com/Blogs/scott/archive/2012/08/14/a-troubleshooting-guide-for-entity-framework-connections-amp-migrations.aspx) if anyone care to read some more. thank you. – Shlo Oct 28 '12 at 14:56
  • 2
    Don't ever delete the mdf files manually if you are using Visual Studio Express. You will face a reinstall, since you don't have access to a lot of the tools to remedy the situation. – DarkNeuron Jun 23 '13 at 01:48
4

Try deleting it again from SQL Management Studio. It'll complain that there's no physical file any more but will remove from the object browser (You'll see after you refresh it)

pangular
  • 699
  • 7
  • 27
4

Remove this line from the connection string that should work ;)

"AttachDbFilename=|DataDirectory|whateverdatabasename-20130917064511.mdf"

Ahmad Moussa
  • 1,296
  • 18
  • 22
1

exit localhost iis express and try again.

Vijayant Katyal
  • 642
  • 2
  • 12
  • 22
0

I delete my DBContext class and re created it again in my MVC Project. Then .mdf file is re created in App_Data

Ahamed Ishak
  • 972
  • 11
  • 16