ASP.NET MVC4 Code First - 'Cannot attach the file as database' exception database ef-code-first entity-framework


When I launch my application while using Entity Framework's Code First principle, I consistently get the following exception:

Cannot attach the file 'C:\Users\Admin\Documents\Visual Studio 2012\
Projects\Pro\Pro.Web\App_Data\Pro.mdf' as database 'Pro'.

I've included this.Global.asax.cs but also unsuccessfully:

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ProWebContext>());
        var ctx = new ProWebContext();

There is no database there, according to my inspection of the App Data directory. Additionally, there is nothing under Data Connections in Server Explorer. Yesterday, everything worked without a hitch, but now, nothing does. When I attempted to connect to LocalDB using SQL Server Management Studio, it failed with a connection error. Any ideas as to what the issue might be?


3/29/2013 11:01:01 AM

Accepted Answer

I discovered the answer. The most recent version of SQL Server Management Studio makes it simple to connect to a local database. The following connection has to be made:

enter image description here

Even though there is nothing in the App Data directory or under Data Connections in Visual Studio's Server Explorer, we can still see the previous database after login in. There won't be any more issues while attaching the database when SQL Server Management Studio is deleted and the application is restarted.

3/31/2013 5:00:38 PM

Popular Answer


Related Questions


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