简体   繁体   中英

“Cannot attach the file” error when deleting a database from the App_Data folder with LocalDb in MVC?

I am using LocalDb and I just want to delete my database.

I was under the impression that due to the way that LocalDb works I would just be able to access the *.mdf and *.ldf files that the local DB instance has created in my App_Data folder (I am using MVC5 by the way).

And that next time I start the application and use it they would just create new files and I would have a new database. But when I delete the files I get the following message...

Cannot attach the file 'c:\\users\\exitos\\documents\\visual studio 2013\\Projects\\xxxxxxxxxxxxxxxxxxxxxx\\xxxxxxxxxxxxxxxxxxxxxx\\App_Data\\aspnet-xxxxxxxxxxxxxxxxxxxxxx-20140118101721.mdf' as database 'aspnet-xxxxxxxxxxxxxxxxxxxxxx-20140118101721'.

Why? I did a reset to my initial commit using GIT to make sure the entire solution folder was 'box fresh' and I still get the problem. I even stopped the Sql Server Express from running and killed all Sql processes and still the same problem.

Is the .ldf and .mdf file written to the registry or something?

I changed the name of the file in the .config and it worked but I don't want to have to keep doing that. I just want to tear down my database by physically deleting it...

This is not obvious but it is advised that you should not delete the physical .mdf and .log files outside of the SQL Server Object Explorer in Visual Studio or in SQL Server Management Studio.

If this happens you can un-register it by using the object explorer.

Go to Server Explorer > data connections, find the db and delete it from there.

在此处输入图片说明

Visual studio will then re-create the db next time without an issue.

I learnt about this from a video by Scott Allen but he's also documented it here.

http://odetocode.com/blogs/scott/archive/2012/08/15/a-troubleshooting-guide-for-entity-framework-connections-amp-migrations.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM