无法将文件* .mdf作为数据库附加 [英] Cannot attach the file *.mdf as database

查看:166
本文介绍了无法将文件* .mdf作为数据库附加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我遵循了一个教程,并决定删除 .mdf 文件。

Basically I've followed a tutorial and decided to delete the .mdf file afterwards.

我尝试运行应用程序我得到以下错误(该线程的标题)。
我收到错误的代码如下所示(ASP.NET MVC 4):

Now whenever I try to run the application I get the following error (the title of this thread). The code where I get the error is shown below (ASP.NET MVC 4):

OdeToFoodDB db = new OdeToFoodDB();

public ActionResult Index()
{
    var model = db.Restaurants.ToList();
    return View(model);
}

我的连接字符串如下:

<add name="DefaultConnection" 
     connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=OdeToFoodDb;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\OdeToFoodDb.mdf" 
     providerName="System.Data.SqlClient" />

我试过查看SQL Server对象资源管理器,但看起来如下:

I've tried looking at the SQL Server Object Explorer but it looks the following:

此外,在服务器资源管理器中我看不到任何数据连接。

Also, in Server Explorer I don't see any data connections.

当我尝试添加一个新的连接服务器资源管理器我没有看到任何名为的数据库OdeToFoodDb

And when I try to add a new connection in Server Explorer I don't see any databases named OdeToFoodDb.

对不起,这个广泛的问题,但我是新的到实体框架,不要在这里找到什么问题。

Sorry for this wide question but I'm new to Entity Framework and don't quite get what's wrong here.

推荐答案

看看这个:实体框架不创建数据库


我会尝试给数据库一个
不同的名字。有时您可能遇到与SQL Express
的问题,尝试创建一个具有相同名称的数据库第二次。
有一种使用SQL Server Management Studio解决这个问题的方法,但是
通常更容易使用不同的数据库名称。

I would try giving the database a different name. Sometimes you can run into problems with SQL Express when trying to create a database with the same name a second time. There is a way to fix this using SQL Server Management Studio but it's generally easier to just use a different database name.

编辑
此答案被接受,因为它确认了错误和OP使用的解决方法(重命名数据库可以帮助)。我完全同意,重命名数据库并不是一个可以接受的方式,而并不能完全解决问题。不幸的是,我没有检查其他方式在SSMS中真正解决它。

Edit This answer was accepted because it confirms the bug and the workaround used by OP (renaming database could help). I totally agree that renaming the database is not really an acceptable way, and does not totally solve the issue. Unfortunatly I didn't check the other ways to really solve it in SSMS.

这篇关于无法将文件* .mdf作为数据库附加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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