Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF5: Cannot attach the file ‘{0}' as database '{1}'

I'm encountering the exact issue as described here (read section "Cannot Attach to Deleted MDF File"), but the solution to the problem is not told there...

In short the issue is that after deleting the .mdf file, the following exception is thrown when I try to access the DB using EF 5.0.

DataException->EntityException->SqlException:
Cannot attach the file ‘{0}' as database '{1}'

I did delete the DB file and now I get that nasty error message when running the application expecting it to use it's initializer. Any way to fix this?

like image 295
Shimmy Weitzhandler Avatar asked Nov 07 '12 17:11

Shimmy Weitzhandler


2 Answers

If you delete the DB file, it still stays registered with SqlLocalDB. Sometimes it fixes it to delete the DB. You can do this from the command line.

  1. Open the "Developer Command Propmpt for VisualStudio" under your start/programs menu.
  2. Run the following commands:

    sqllocaldb.exe stop v11.0

    sqllocaldb.exe delete v11.0

like image 94
CodingWithSpike Avatar answered Sep 22 '22 08:09

CodingWithSpike


For those still searching for a solution...

Go to View / SQL Server Object Explorer and delete the database from the (localdb)\v11.0 subnode!
enter image description here

There is essentially a local database retained of where the files should be, and if you want to delete the database files make sure you remove them from this explorer utility, not manually.

like image 29
JSobell Avatar answered Sep 25 '22 08:09

JSobell