我有一個數據庫優先ASP.NET MVC 4項目,並希望使用SimpleMembershipProvider與我的數據庫中的自定義user_info表。我正在使用Entitiy Framework 6與SQL Server。我已經搜索了互聯網,因此尋求解決方案,但沒有運氣。使用SimpleMembershipProvider與我自己的數據庫在數據庫優先ASP.NET MVC4項目
我USER_INFO表看起來像這樣:
[Id] INT IDENTITY (1, 1) NOT NULL,
[email] NVARCHAR (MAX) NULL,
[first_name] NVARCHAR (MAX) NULL,
[last_name] NVARCHAR (MAX) NULL,
[internal_role] INT NULL,
CONSTRAINT [PK_user_info] PRIMARY KEY CLUSTERED ([Id] ASC)
我試圖按照 How can I customize simple membership provider to work with my own database ASP.NET mvc 4的步驟,但我不認爲這個方法對非代碼優先項目。我收到服務器錯誤「無法找到請求的.Net Framework數據提供程序,它可能沒有安裝。」當我嘗試將下面的代碼添加到函數SimpleMembershipInitializer()。
WebSecurity.InitializeDatabaseConnection("ReservationSystemEntities", "user_info", "Id", "email", autoCreateTables: true);
這裏是我的web.config文件中的相關信息:
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=xxxxxx" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="ReservationSystemEntities" connectionString="metadata=res://*/ReservationSystem.csdl|res://*/ReservationSystem.ssdl|res://*/ReservationSystem.msl;provider=System.Data.SqlClient;provider connection string="data source=(localdb)\v11.0;initial catalog=ReservationSystem;integrated security=True;user id=xxxxx;password=xxxxx;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
我甚至不知道這是可能的,基於這樣的回答:SimpleMembershipInitializer won't initialize
在此基礎上回答Using MVC 4 SimpleMembership with an existing database-first EF model我需要指定一個像這樣的連接字符串:
<add name="CONNECTION_STRING_NAME" connectionString="data source=SERVER;initial catalog=DATABASE;user id=USER;password=PASSWORD;" providerName="System.Data.SqlClient" />
我是確定如何將連接字符串轉換爲像這樣的連接字符串。這種類型的連接字符串是否僅在使用Code-First時有效?
任何幫助將不勝感激。
EF6可以對SQL Server,緊湊型SQL服務器,SQL Server Express的,或MySQL運行。 您正在使用哪種數據庫服務器類型? 此外,請發佈您的webconfig中的EntityFramework和System.Data部分「忽略密碼/用戶名」 您正在獲得的錯誤是EF6未在web.config中正確配置。或者您缺少EF6的nuget軟件包。 如果您使用的是MySql,有很多技巧可以讓它運行良好。 –
@Ryios非常感謝你的幫助。我已經用你所要求的信息更新了這個問題。嘗試使用SimpleMembership和我自己的數據庫之前,我的網站正常使用EF6。直到我開始嘗試將WebSecurity.InitializeDatabaseConnection(...)部分添加到InitializeSimpleMembershipAttribute時,我才能夠使用EF6訪問包含user_info表的數據庫,而不會出現問題。 – oalbrecht
我在自己的數據庫中使用SimpleMembership信息開始了我的項目。除了這個數據庫之外,我還有一個擁有使用EF6訪問的所有表格的數據庫。現在我想停止對包含SimpleMembership表的數據庫使用DefaultConnection,而是使用SimpleMembership我的其他數據庫。 – oalbrecht