Reputation: 3045
ERROR:
Cannot attach the file 'C:\Users\dboyle\Desktop\New folder (2)\OilNGasWeb\OilNGasWeb\App_Data\aspnet-MvcApplication4-20130726115749.mdf' as database 'aspnet-MvcApplication4-20130726115749'.
*This error occured after deleting my Web.config file *( replacing it with a new one from a new project and replacing the default conection string with what i needed)
EDIT:
Sofar I have tried deleting the .mdf physical file and from VS, also from SQL. Then re-running the update from the package manager within VS. The database gets made, yet same error persists.
I have found
<ObjectGroup Name="DefaultConnection" Order="1" Enabled="False">
<Destination Path="Data Source=ANE-SQL\ANESQLSERVER;Initial Catalog=OilGas;User ID=software;Password=GLvp$102" />
<Object Type="DbCodeFirst">
<Source Path="DBMigration" DbContext="OilNGasWeb.Models.OilNGasDB, OilNGasWeb" MigrationConfiguration="OilNGasWeb.Migrations.Configuration, OilNGasWeb" Origin="Configuration" />
</Object>
</ObjectGroup>
in my website.pubxml file ( it still shows my old database name ) will this interfier?
Upvotes: 3
Views: 4544
Reputation: 6761
I had the same problem.
I used Visual Studio 2015 and SQL Server LocalDB v12 so the answer from the accepted answer didn't work for me. I used these commands instead (without version) and it worked:
sqllocaldb.exe stop
sqllocaldb.exe delete
Upvotes: 0
Reputation: 3045
While doing database update using code-first migrations in ASP.Net MVC, came across the strange exception and details are as follows,
Issue back ground details,
Manually deleted auto created ".mdf" file from App_Data folder using Visual Studio.
Executed update-database in package manager console. Then got the below exception,
System.Data.SqlClient.SqlException (0x80131904): Cannot attach the file 'E:\Backup\Practice\MVC4\DotNetExamples\DotNetExamples\App_Data\DotnetExamples.mdf' as database 'DotnetExamples'.
Solution:
If you delete the DB file, it still stays registered with SqlLocalDB. Sometimes it fixes it by deleting DB. We can do this from the command line.
Open the "Developer Command Propmpt for VisualStudio" under your "Start/Programs menu->All Programs->Visual Studio 2012->Visual Studio Tools"
Run the following commands:
sqllocaldb.exe stop v11.0
sqllocaldb.exe delete v11.0
Please have the .mdf file and SSMS instance of the Database Deleted. Now execute "update-database" command from package manager console and it will create database for you without any obstacles.
Upvotes: 4
Reputation: 1471
You probably have an attached db with the same name in your SQL Server, just open up SQL remove it and run your MVC4 again.
Upvotes: 1