2016-09-18 62 views
2

我想在MVC4中使用註冊和登錄表單。我已經使用實體框架從數據庫創建模型,現在我想生成成員表,但我已經嘗試了很多東西,我仍然無法訪問它們。如何訪問mvc4中的成員資格表?

我有2個連接字符串:

<connectionStrings> 
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-Projekti-20160917211151;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-Projekti-20160917211151.mdf" providerName="System.Data.SqlClient" /> 

<add name="UniversitetiEntities" connectionString="metadata=res://*/Models.Universiteti.csdl|res://*/Models.Universiteti.ssdl|res://*/Models.Universiteti.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=ARBERS-PC\BERSANTA;initial catalog=Universiteti;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /> </connectionStrings> 

帳號模型我改變了UniversitetiEntities基地:

public class UsersContext : DbContext 
{ 
    public UsersContext() 
     : base("UniversitetiEntities") 
    { 
    } 

    public DbSet<UserProfile> UserProfiles { get; set; } 
} 

[Table("UserProfile")] 
public class UserProfile 
{ 
    [Key] 
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 
    public int UserId { get; set; } 
    public string UserName { get; set; } 
} 

我也做這個昌InitializeSimpleMembershipAttribute文件:

private class SimpleMembershipInitializer 
    { 
     public SimpleMembershipInitializer() 
     { 
      Database.SetInitializer<UsersContext>(null); 

      try 
      { 
       using (var context = new UsersContext()) 

       { 
        if (!context.Database.Exists()) 
        { 
         // Create the SimpleMembership database without Entity Framework migration schema 
         ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); 
        } 
       } 

       WebSecurity.InitializeDatabaseConnection("UniversitetiEntities", "UserProfile", "UserId", "UserName", autoCreateTables: true); 
      } 
      catch (Exception ex) 
      { 
       throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); 
      } 
     } 
    } 
} 

但它總是拋出這個異常:ASP.NET簡單成員資格數據庫c不能初始化。

回答

1

AFAIK,SimpleMembershipProvider用戶配置文件管理僅適用於SQL Server數據庫連接字符串,不是EF連接字符串。

首先,可以嘗試設置你的數據庫連接字符串,而不是EF-產生的連接字符串作爲陳述here

private class SimpleMembershipInitializer 
{ 
    public SimpleMembershipInitializer() 
    { 
     Database.SetInitializer<UsersContext>(null); 

     try 
     { 
      using (var context = new UsersContext()) 
      { 
       if (!context.Database.Exists()) 
       { 
        // Create the SimpleMembership database without Entity Framework migration schema 
        ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); 
       } 
      } 

      WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true); 
     } 
     catch (Exception ex) 
     { 
      throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); 
     } 
    } 
} 

// also set your DB connection string on current DB context 
public class UsersContext : DbContext 
{ 
    public UsersContext() 
     : base("DefaultConnection") 
    { 
    } 

    public DbSet<UserProfile> UserProfiles { get; set; } 
} 

如果第一個解決方案將無法正常工作,請嘗試從SQL Server連接字符串刪除AttachDbFileName屬性:

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-Projekti-20160917211151;Integrated Security=SSPI" providerName="System.Data.SqlClient" /> 

注意:如果你的數據庫連接具有一定的用戶ID &密碼才能訪問,包括他們在DefaultConnection

根據您的EF模型生成(代碼優先,數據庫優先或模型優先方法),您可以根據UserProfile表的存在情況通過true或false設置autoCreateTables:

更多信息:SimpleMembership Provider Reference

+0

我改變它在「DefaultConnection」但我仍然得到這個錯誤:「System.InvalidOperationException」類型的異常出現在Projekti.dll但在用戶代碼中沒有處理 –

+0

請編輯您的問題包括你的錯誤,因爲'InvalidOperationException'可能由應用的某些條件觸發,指定異常詳細信息和堆棧跟蹤(如果有的話)。 –