2013-05-26 28 views
1

我已經使用EF 5 Code First創建了我的應用程序域模型。然後,我試圖通過運行aspnet_reqsql.exe實用程序來在ASP.NET數據庫中生成成員資格表,將其與ASP.NET默認成員資格提供程序掛鉤。使用ASP.NET默認成員身份與EF 5代碼優先未找到連接字符串「DefaultConnection」

這似乎工作正常,因爲當我在SQL Management Studio中檢查數據庫時,所有成員表都與我的Code First表一起存在。

現在,我們運行我的應用程序,一切看起來不錯,它加載,我可以瀏覽頁面。但是,只要我瀏覽需要成員資格的內容(例如登錄頁面),應用程序就會中斷。我得到的死亡的黃色畫面,以下消息:

Server Error in '/' Application. 

Connection string "DefaultConnection" was not found. 

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: Connection string "DefaultConnection" was not found. 

Source File: c:\Users\XXX\Documents\Visual Studio 2012\Projects\YourApp\YourApp\Filters\InitializeSimpleMembershipAttribute.cs Line: 41 

this thread使面臨類似問題的用戶,但是他的解決方案已經被我實現,事實上,我不得不註釋掉已經web.config中的現有連接字符串。我已經做到了,但仍然沒有成功。

我在做什麼錯?我將不勝感激任何幫助。

感謝

回答

2

當你打電話InitializeSimpleMembershipAttribute,如果你仍然有"DefaultConnection"作爲第一個參數,那麼你需要改變這是你想要使用的連接的名稱。

+0

謝謝理查德。 – Ciwan

0

我設法理清了這一切都歸功於理查德。下面是我做的:

我打開了我的AccountController,在那裏類的頂部,你會看到這個(如果使用ASP.NET MVC4):

[Authorize] 
[InitializeSimpleMembership] 
public class AccountController : Controller 
{ 
    // all code for controller here. 
} 

我然後右鍵單擊在[InitializeSimpleMembership]並點擊Go To Definition

一旦出現,如果您滾動至底部,您會看到這樣的事情:

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); 

變化DefaultConnection屬性,無論你已經命名的EF代碼第一個上下文,你是好走。

謝謝理查德。