I have just started learning ASP.NET MVC 5. As a jumping off point I am working though Rick Anderson’s Getting Started with ASP.NET MVC 5. I am completely new to ASP.NET and Entity Framework and Rick’s tutorial has been a great introduction. Everything was going great until I got to the Add a New Field section.
The tutorial is using entity framework 6 to interact with an instance of SQL Express LocalDB. As part of adding a new field to an existing model Rick walks the reader through enabling code first migrations, creating an initial migration and deleting the existing database file. I completed all the steps without any issues.
The next step was to run Update-Database from the package manager console. This command failed with the message “Cannot attach the file ‘path to .mdf’ as database ‘name of database'”. After some searching I found out that using the solution explorer to manually deleting a mdf file from the App_Data directory has been known to cause problems. I restored my mdf from the recycle bin and fired up Visual Studio’s SQL Server Object Explorer and connected using a connection string of “(LocalDB)\v11.0” which I had been using in my application.
As you can see from this screenshot the only databases in this instance of LocalDB are system databases. I ran the application and to my surprise it was working again after the restore the mdf file. I tried disconnecting LocalDB and reconnecting with the same results. I tried using the package manager to stop and delete LocalDB. None of this cleared up the problems I was having with the Update-Database command. Some of the post I can across suggested using a different database name in the application’s connection string so that a completely new database would be created, but I was only going to use that tactic as a last resort.
After more searching I came across a post that was using a different connection string for LocalDB. Instead of “(LocalDB)\v11.0” that I had seen referenced in all of the examples I had seen so far this person was using “(LocalDB)\MSSQLLocalDB”. As it turns out “(LocalDB)\MSSQLLocalDB” is the connection string used for SQL Express 2014 and “(LocalDB)\v11.0” is used for SQL Express 2012. I have both the 2012 and 2014 version installed on my machine. I am not clear on why my database was being created in the 2014 version of SQL Express when my application was using the 2012 connection string. As a fix I have updated all the connection strings in my application to the 2014 version and all is now working without any more problems.
You said: ” I have updated all the connection strings in my application to the 2014 version “. How did you do that? I have the same problem and have came across a lot of solutions which has led to this mix of ideas where I don’t know what would be, specifically, the right thing to do. I too have Sqlserver 2012 and 2014 versions installed.
Ammar find “(LocalDB)\v11.0” and replace it with “(LocalDB)\MSSQLLocalDB” was the fix for my problems.
Replacing that in connections string does the task. But i have read somewhere that there are settings in visual studio where you can set the default sql server. You got any idea about that?
I looked around a bit and did not see a setting. My guess is it is defined as part of the project template.
I have been doing some research and I am still quite unclear about many things but I think we are always supposed to use MSSQLLocalDB and that we set it to point to V11 or V12 or V13 or whatever. The problem is that what I read says to use SSDT and other utilities that I don’t have but the main thing is that MSSQLLocalDB apparently is not a specific version.
I am finished with programming of every kind… my days are ending. It was nice exploring and learning these things.. took me a lot of effort though to grasp these concepts. it was nevertheless very intriguing. and I was a CS graduate haha