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

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

编辑这个答案被接受了,因为它确认了 bug 和 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天全站免登陆