ASP.NET MVC4代码首先 - “无法附加文件作为数据库”异常 [英] ASP.NET MVC4 Code First - 'Cannot attach the file as database' exception

查看:251
本文介绍了ASP.NET MVC4代码首先 - “无法附加文件作为数据库”异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Entity Framework中使用Code First概念,并且我在启动应用程序时总是遇到以下异常:

I'm using Code First concept in Entity Framework and I'm constantly getting the following exception while starting application:

Cannot attach the file 'C:\Users\Admin\Documents\Visual Studio 2012\
Projects\Pro\Pro.Web\App_Data\Pro.mdf' as database 'Pro'.

我把它放在 Global.asax.cs 但不成功:

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ProWebContext>());
        var ctx = new ProWebContext();
        ctx.Database.Initialize(true);

我在App_Data目录下检查过,没有创建数据库。此外,在服务器资源管理器下,数据连接下没有任何一切都工作正常昨天和今天不工作。我试图连接到LocalDB与SQL Server管理工作室,但它说它不能连接到本地数据库。任何想法可能是一个问题?

I've checked under App_Data directory and there no database created. Also, under Server Explorer there is nothing under Data Connections. Everything was working fine yesterday and today not working at all. I've tried to connect to LocalDB with SQL Server Management studio but it says it cannot connect to local database. Any ideas what could be a problem?

谢谢!

推荐答案

我找到了解决方案。使用最新的SQL Server Management studio,在连接到本地数据库时没有问题。需要建立以下连接:

I found the solution. With newest SQL Server Management studio there is no problem in connecting to the local database. Connection needs to be established like this:

登录后,我们仍然可以看到旧的数据库存在,即使没有什么在App_Data目录下,在Visual Studio中的服务器资源管理器中的数据连接下。当我们从SQL Server Management studio中删除该数据库并再次启动应用程序时,在附加数据库时不会再有错误。

After logging in we can still see old database present even if there is nothing under App_Data directory and under Data Connections in Server Explorer in Visual Studio. When we delete that database from SQL Server Management studio and start application again there will be no more errors while attaching database.

这篇关于ASP.NET MVC4代码首先 - “无法附加文件作为数据库”异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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