我創建了一個新的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
@Eonasdan,一年後,是的。請看我的答案。 – joelmdev