【发布时间】:2012-08-25 16:05:49
【问题描述】:
我创建了一个新的 ASP.NET MVC 4 应用程序,并希望它首先使用代码。但是,如果它不存在,它似乎最初不会创建数据库文件。如果我从 App_Data 文件夹中删除 .mdf 文件,那么当应用程序尝试访问数据库时会出现以下异常:
System.Data.SqlClient.SqlException: Cannot attach the file '<path-to-db-file>.mdf' as database '<my-db-file-name>'.
如果我在调试器的应用程序中运行它,那么我可以看到在调用 LazyInitializer.EnsureInitialized 时 InitializeSimpleMembershipAttribute::OnActionExecuting 方法中发生了异常。捕获的异常是:
[System.Reflection.TargetInvocationException] {"Exception has been thrown by the target of an invocation."} System.Reflection.TargetInvocationException
内部例外:
[System.InvalidOperationException] {"The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588"} System.InvalidOperationException
然后有我上面提到的第一个异常作为它的内部异常。
任何想法我做错了什么?
更新
我刚刚尝试了一个全新的 MVC4 应用程序。我可以通过执行以下操作来复制它:
- 在 VS 向导中创建 MVC 应用程序。
- 第一次运行应用程序并进入登录页面(注意现在生成了 mdf 文件)。
- 删除mdf文件,返回登录页面。现在抛出异常。
【问题讨论】:
-
你有想过这个问题吗?
-
@Eonasdan,一年后,是的。请看我的回答。
标签: c# asp.net-mvc entity-framework entity-framework-4 ef-code-first