asp.net-mvc 从 app_data 中删除 .mdf 文件导致异常无法将文件附加为数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13001441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Delete .mdf file from app_data causes exception cannot attach the file as database
提问by Shlo
I am building a web application using VS 2012 MVC4 code first. In order to recreate the .mdffile after I changed the model, I manually deleted the file from the app_data directory in VS. I have done this a few times before without any problem.
我首先使用 VS 2012 MVC4 代码构建一个 Web 应用程序。为了.mdf在我更改模型后重新创建文件,我从VS中的app_data目录中手动删除了该文件。我以前做过几次,没有任何问题。
Now I receive an exception:
现在我收到一个异常:
The underlying provider failed on Open. ==> Cannot attach the file
MYDB.mdfas database'MYDB'.
底层提供程序在 Open 上失败。==> 无法将文件附加
MYDB.mdf为数据库'MYDB'。
I will appreciate your help on how to recreate the .mdffile.
我将感谢您对如何重新创建.mdf文件的帮助。
Thank you!
谢谢!
采纳答案by tdykstra
Use SQL Server Object Explorer to delete the database rather than just deleting the .mdf file in Solution Explorer. Otherwise the SQL Server instance still thinks the database name is used and the next time you try to attach a database to the same name it won't work. See the section on Initial Catalog in this MSDN page:
使用 SQL Server 对象资源管理器删除数据库,而不仅仅是删除解决方案资源管理器中的 .mdf 文件。否则 SQL Server 实例仍然认为使用了数据库名称,下次您尝试将数据库附加到相同名称时,它将不起作用。请参阅此 MSDN 页面中有关初始目录的部分:
http://msdn.microsoft.com/en-us/library/jj653752.aspx#initialcatalog
http://msdn.microsoft.com/en-us/library/jj653752.aspx#initialcatalog
回答by Gleb Esman
That what fixed it for me, From Package Manager Console run these commands:
那是什么为我修复了,从包管理器控制台运行这些命令:
sqllocaldb.exe stop v11.0
sqllocaldb.exe delete v11.0
And then:
进而:
Update-Database
回答by pangular
Try deleting it again from SQL Management Studio. It'll complain that there's no physical file any more but will remove from the object browser (You'll see after you refresh it)
尝试从 SQL Management Studio 中再次删除它。它会抱怨不再有物理文件,但会从对象浏览器中删除(刷新后您会看到)
回答by Ahmad Moussa
Remove this line from the connection string that should work ;)
从应该工作的连接字符串中删除这一行;)
"AttachDbFilename=|DataDirectory|whateverdatabasename-20130917064511.mdf"
“AttachDbFilename=|DataDirectory|whateverdatabasename-20130917064511.mdf”
回答by Vijayant Katyal
exit localhost iis express and try again.
退出 localhost iis express 并重试。
回答by Ahamed Ishak
I delete my DBContext class and re created it again in my MVC Project. Then .mdf file is re created in App_Data
我删除了我的 DBContext 类并在我的 MVC 项目中重新创建了它。然后在 App_Data 中重新创建 .mdf 文件

