如何在mvc4中的成员资格表中访问?

时间:2016-09-18 19:01:54

标签: c# asp.net asp.net-mvc entity-framework asp.net-mvc-4

我正在尝试在MVC4中使用注册表和登录表单。我已经使用DB中的Entity Framework来创建模型,现在我想生成成员资格表,但我尝试了很多东西,但我仍然无法访问它们。

我有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 Simple Membership数据库。

1 个答案:

答案 0 :(得分:1)

AFAIK,SimpleMembershipProvider用户配置文件管理仅适用于SQL Server数据库连接字符串,而不适用于EF连接字符串。

首先,尝试设置数据库连接字符串,而不是按照here所述设置EF生成的连接字符串:

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&amp;要获取访问权限的密码,请将其包含在DefaultConnection

根据您的EF模型生成(代码优先,数据库优先或模型优先方法),您可以根据autoCreateTables:表的存在将UserProfile设置为true或false。

更多信息:SimpleMembership Provider Reference