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

Sql Serverasp.net MvcVisual Studio-2012Entity Framework-5Localdb

Sql Server Problem Overview


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?

Sql Server Solutions


Solution 1 - Sql Server

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

Solution 2 - Sql Server

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.

Solution 3 - Sql Server

I did try JSobell's solution first but did not see my database listed there. I ran CodingWithSpike's commands from VS Developer Command Prompt, but that did not work either. Finally I ran CodingWithSpike's same commands from Package Manager Console and that worked.

    PM> sqllocaldb.exe stop v11.0
    LocalDB instance "v11.0" stopped.

    PM> 
    PM> sqllocaldb.exe delete v11.0
    LocalDB instance "v11.0" deleted.
    
    PM> sqllocaldb.exe start v11.0
    LocalDB instance "v11.0" started.

Solution 4 - Sql Server

Alright.

My solution was simple, I changed to use local server:

I changed the DataSource attribute in the connection string from:

Data Source=(LocalDb)\v11.0;blah

To:

Data Source=.\SQLEXPRESS;blah

Another solution is login to LocalDb via SQL Management Studio, and try to delete that database:

enter image description here

However it didn't work for me, when I try to delete it it says "TITLE: Microsoft SQL Server Management Studio

>The Database '{0}' does not exist on the server. (SqlManagerUI)

When I try to detach it the database doesn't appear in the list for detach selection, "Take offline" also takes me to the error above.

Which leads me to think this is a solid bug in LocalDB.

Solution 5 - Sql Server

The easiest fix is to simply change the name of your DB in the connection string. See Rowan Millers blog How to Drop a Database from Visual Studio 2012 for alternate solutions. We hope to fix this problem in a future edition.

Solution 6 - Sql Server

The best and easy answer I just solved it now, Just use ur sql server name as data source, initial catalog be your database name and there you go remove the mdf line

Solution 7 - Sql Server

In my case I use migrations and in config i simply changed name of dataContext and dataContext class itself (just rename), then try again and that helped

Solution 8 - Sql Server

For SQL 2014 please follow CodingWithSpike selected answer and this comment

> As a side-note, the v11.0 is specific to SQL LocalDB 2012. If you are > using LocalDB 2014, MS renamed it to MSSqlLocalDb instead. – > CodingWithSpike Aug 29 '14 at 19:20

Solution 9 - Sql Server

I had the same problem. I ran the following commands in the package manager console and it fixed the problem

sqllocaldb.exe stop MSSqlLocalDb

sqllocaldb.exe delete MSSqlLocalDb

Solution 10 - Sql Server

I had the same problem and I solved it by manually setting the "DataDirectory" folder to another folder in my app binaries.

I put this line in the Global.asax Application_Start method:

AppDomain.CurrentDomain.SetData("DataDirectory", Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "App_Data"));

My connection string is currently set to this:

<connectionStrings>
    <add name="DataContext" connectionString="Data Source=(LocalDb)\v11.0; Initial Catalog=DataContext; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|DataContext.mdf"
  providerName="System.Data.SqlClient" />

Solution 11 - Sql Server

I could fix it by renaming DataBase name in my connection String, from the default aspnet-{numbers} to a simple name, it worked.

Solution 12 - Sql Server

Connect to (LocalDb)\v11.0 using Sql server management studio, delete the db and then do an update-database in package manager console.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionShimmy WeitzhandlerView Question on Stackoverflow
Solution 1 - Sql ServerCodingWithSpikeView Answer on Stackoverflow
Solution 2 - Sql ServerJSobellView Answer on Stackoverflow
Solution 3 - Sql ServerAJsView Answer on Stackoverflow
Solution 4 - Sql ServerShimmy WeitzhandlerView Answer on Stackoverflow
Solution 5 - Sql ServerRickAndMSFTView Answer on Stackoverflow
Solution 6 - Sql ServergiftedView Answer on Stackoverflow
Solution 7 - Sql Serverno0bassssView Answer on Stackoverflow
Solution 8 - Sql ServerThe OneView Answer on Stackoverflow
Solution 9 - Sql ServerTony NewsomView Answer on Stackoverflow
Solution 10 - Sql ServerFilipe ScurView Answer on Stackoverflow
Solution 11 - Sql ServerEdgar FroesView Answer on Stackoverflow
Solution 12 - Sql ServerNidhinView Answer on Stackoverflow