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

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

问题描述

我使用实体框架code第一个概念,我经常收到以下异常,同时启动应用程序:

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目录下检查,有没有数据库创建。此外,在服务器资源管理器没有任何根据的数据连接。一切都被罚款昨天工作,今天不工作。我试图与SQL Server Management Studio连接到的LocalDB,但它说,它无法连接到本地数据库。任何想法可能是什么问题呢?

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管理工作室中有连接到本地数据库中没有问题。连接需要这样来建立:

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服务器资源管理器数据连接旧数据库present后。当我们删除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 code首先 - “无法附加文件作为数据库'例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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