【问题标题】:Entity Framework Seeding with Identity (Microsoft.Owin.Security) user具有身份的实体框架播种 (Microsoft.Owin.Security) 用户
【发布时间】:2013-12-24 01:18:05
【问题描述】:

我有一个类为数据库添加 2 个具有角色和自定义字段的用户。我遇到的问题是它将数据保存在 [dbo].[AspNetUsers] 而不是 [dbo].[IdentityUsers] 中。两个表都已创建。播种后,数据进入 AspNetUser。当我启动网站并注册新用户时,数据会进入 IdentityUser。

这是迁移类:

internal sealed class Configuration : DbMigrationsConfiguration<DatabaseContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
    }

    protected override void Seed(DatabaseContext context)
    {
        base.Seed(context);

        var userStore = new UserStore<ApplicationUser>();
        var manager = new UserManager<ApplicationUser>(userStore);

        var role = new IdentityUserRole { Role = new IdentityRole(Model.Roles.ADMINISTRATOR) };
        var user = new ApplicationUser() { UserName = "123123", Email = "123123@123.com", Language = "en-US"};
        user.Roles.Add(role);
        IdentityResult result = manager.Create(user, "123123");

        var role2 = new IdentityUserRole { Role = new IdentityRole(Model.Roles.NORMAL) };
        var user2 = new ApplicationUser() { UserName = "qweqwe", Email = "qweqwe@qweqwe.com", Language = "fr-CA" };
        user.Roles.Add(role2);
        IdentityResult result2 = manager.Create(user2, "qweqwe");
    }
}

这是为 Identity 模型定义自定义字段的 ApplicationUser 类。

public class ApplicationUser : IdentityUser, ICurrentUser
{
    public ApplicationUser()
    {
        Email = "";
        Language = "";
    }
    public string UserId {
        get { return base.Id; }
        set{} 
    }
    public string Email { get; set; } 
    public string Language { get; set; } 

}

这是这个新类的实体框架配置类。

public class ApplicationUserConfiguration : EntityTypeConfiguration<ApplicationUser>
{
    public ApplicationUserConfiguration()
    {
        this.HasKey(d => d.Id);
        this.Ignore(d => d.UserId);
    }
}

两者都使用相同配置的保存数据上下文:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        //... others entity here

        modelBuilder.Configurations.Add(new ApplicationUserConfiguration());

        //Theses configuration are required since custom fields are added to ApplicationUser. Here is why : http://stackoverflow.com/questions/19474662/map-tables-using-fluent-api-in-asp-net-mvc5-ef6
        modelBuilder.Entity<IdentityUserLogin>().HasKey(l => l.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });

    }

我的问题是:为什么我的 AspNet 前缀表名称与身份表重复?为什么种子使用一个而 Web 应用程序使用另一个?

【问题讨论】:

    标签: asp.net-mvc entity-framework entity-framework-6 asp.net-identity


    【解决方案1】:

    您的 DbContext 是否继承自 IdentityDbContext?

    如果您从 IdentityDbContext 继承,您不需要在 OnModelCreating 中进行任何额外配置。 IdentityDbContext 提供的映射和默认的实体框架约定就足够了。

    如果您声明您的 ApplicationUser 时没有设置 UserId,那么您将不需要忽略 UserId 属性:

    public class ApplicationUser : IdentityUser
    {
        public ApplicationUser()
        {
            Email = "";
            Language = "";
        }
    
        public string Email { get; set; }
        public string Language { get; set; }
    
        public string UserId
        {
            get { return Id; }
        }
    }
    

    那么您的 ApplicationDbContext 可以像这样简单(为了清楚起见,我已将您的 Configuration 类重命名为 MigrationConfiguration):

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        static ApplicationDbContext()
        {
            Database.SetInitializer(new MigrateDatabaseToLatestVersion<ApplicationDbContext, MigrationConfiguration>());
        }
    
        public ApplicationDbContext()
            : base("DefaultConnection")
        {
        }
    }
    

    这与 AspNet.Identity.Core 和 AspNet.Identity.EntityFramework 程序集的发布版本(版本 1.0.0)按预期工作(仅名为 AspNetXXXX 的表,AspNetUsers 表具有自定义字段)。

    【讨论】:

    • 谢谢。我用 IdentityContext 做到了,一切正常。
    猜你喜欢
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 2020-09-12
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多