从app_data删除.mdf文件导致异常不能将文件附加为数据库 [英] Delete .mdf file from app_data causes exception cannot attach the file as database

查看:167
本文介绍了从app_data删除.mdf文件导致异常不能将文件附加为数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VS 2012 MVC4代码来构建一个Web应用程序。为了在更改模型后重新创建 .mdf 文件,我手动从VS中的app_data目录中删除该文件。我已经这样做了几次,没有任何问题。



现在我收到一个例外:


打开时底层提供程序失败。 ==>无法将文件
MYDB.mdf 作为数据库'MYDB'


感谢您对如何重新创建 .mdf 文件的帮助。



谢谢!

解决方案

使用SQL Server对象资源管理器删除而不是在解决方案资源管理器中删除.mdf文件。否则,SQL Server实例仍然认为使用数据库名称,下次尝试将数据库附加到同一个名称时将不起作用。请参阅此MSDN页面中初始目录的部分:



http://msdn.microsoft.com/en-us/library/jj653752.aspx#initialcatalog


I am building a web application using VS 2012 MVC4 code first. In order to recreate the .mdf file 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.

Now I receive an exception:

The underlying provider failed on Open. ==> Cannot attach the file MYDB.mdf as database 'MYDB'.

I will appreciate your help on how to recreate the .mdf file.

Thank you!

解决方案

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:

http://msdn.microsoft.com/en-us/library/jj653752.aspx#initialcatalog

这篇关于从app_data删除.mdf文件导致异常不能将文件附加为数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆