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

查看:268
本文介绍了无法将文件* .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.

推荐答案

看看这个: Entity Framework不创建数据库


我会尝试给数据库一个
不同的名字。有时,当尝试再次使用相同的名称创建数据库时,可能会遇到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天全站免登陆