回答

3

我剛剛經歷了這個過程,其步驟如下。我假設你將使用實體框架進行數據訪問,並將它已經建立了:

  • 參考庫WebMatrix.DataWebMatrix.WebData。你會發現他們 大會在/在添加引用對話框擴展
  • 添加以下部分爲Web.config
<configSections> 
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
    </configSections> 
    <connectionStrings> 
    <add name="DefaultConnection" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=LicenceAudit.mdf;Integrated Security=SSPI;attachDBFilename=|DataDirectory|\LicenceAudit.mdf" providerName="System.Data.SqlClient" /> 
    </connectionStrings> 
    <entityFramework> 
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> 
     <parameters> 
     <parameter value="v11.0" /> 
     </parameters> 
    </defaultConnectionFactory> 
    </entityFramework> 
<system.web> 
<membership defaultProvider="simpleMembershipProvider"> 
     <providers> 
     <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData"/> 
     </providers> 
    </membership> 
    <roleManager enabled="true" defaultProvider="SimpleRoleProvider"> 
    <providers> 
    <clear/> 
    <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/> 
    </providers> 
</roleManager> 
</system.web> 
  • 添加WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UsersTableName", "UserId", "UserName", true)Application_Start()Global.asax.cs
  • 確保您的數據庫文件存在幷包含適當的表格。 UserId屬性的類型應該是int
  • 通過執行WebSecurity.CreateUserAndAccount("testUser", "myStrongPassword")來測試所有內容。如果它通過,你就清楚了。