Avoid 'Discriminator' with AspNetUsers, AspNetRoles, & AspNetUserRoles

岁酱吖の 提交于 2020-01-01 16:09:31

问题


I am extending IdentityUser, IdentityUserRole, and IdentityRole like this:

public class ApplicationUser : IdentityUser
{
    public string FullName { get; set; }

    public virtual ICollection<ApplicationIdentityUserRole> Roles { get; } = new List<ApplicationIdentityUserRole>();
}

public class ApplicationIdentityUserRole : IdentityUserRole<string>
{
    public virtual ApplicationUser User { get; set; }
    public virtual ApplicationRole Role { get; set; }
}

public class ApplicationRole : IdentityRole
    {
        public virtual ICollection<ApplicationIdentityUserRole> Roles { get; } = new List<ApplicationIdentityUserRole>();
    }

and configured like:

public class SmartAccountingSetUpContext : IdentityDbContext<ApplicationUser>
{
    public SmartAccountingSetUpContext(DbContextOptions<SmartAccountingSetUpContext> options)
        : base(options)
    {

    }

    public DbSet<ApplicationUser> Users { get; set; }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);

        builder.Ignore<RegistrationViewModel>();
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);            
        builder.Entity<ApplicationUser>().ToTable("AspNetUsers");
        builder.Entity<ApplicationIdentityUserRole>().ToTable("AspNetUserRoles");
        builder.Entity<ApplicationRole>().ToTable("AspNetRoles");           


        builder.Entity<ApplicationIdentityUserRole>()
                    .HasOne(p => p.User)
                    .WithMany(b => b.Roles)
                    .HasForeignKey(p => p.UserId);

        builder.Entity<ApplicationIdentityUserRole>()
            .HasOne(x => x.Role)
            .WithMany(x => x.Roles)
            .HasForeignKey(p => p.RoleId);
    }
}

I keep getting this:

"Invalid column name 'Discriminator'.\r\nInvalid column name 'Discriminator'.\r\nInvalid column name 'Discriminator'.\r\nInvalid column name 'Discriminator'."

I understand if you have derived class, then you have to specify the HasDiscriminitor in OnModelCreating method. However IdentityUser, IdentityUserRole, and IdentityRole are no abstract classes.

How can I get past this?


回答1:


Your context is inheriting IdentityDbContext<TUser> which in turn inherits IdentityDbContext<TUser, IdentityRole, string>. TUser in this case is your ApplicationUser, but the role type is IdentityRole.

Thus the base class fluent configuration registers IdentityRole as entity. When you register the derived ApplicationRole as entity, EF Core treats that as TPH (Table Per Hierarchy) Inheritance Strategy which is implemented with single table having Discriminator column.

To fix the issue, simply use the proper base generic IdentityDbContext. Since you also have a custom IdentityUserRole derived type, you should use the one with all generic type arguments - IdentityDbContext<TUser,TRole,TKey,TUserClaim,TUserRole,TUserLogin,TRoleClaim,TUserToken>:

public class SmartAccountingSetUpContext : IdentityDbContext
<
    ApplicationUser, // TUser
    ApplicationRole, // TRole
    string, // TKey
    IdentityUserClaim<string>, // TUserClaim
    ApplicationIdentityUserRole, // TUserRole,
    IdentityUserLogin<stringy>, // TUserLogin
    IdentityRoleClaim<string>, // TRoleClaim
    IdentityUserToken<string> // TUserToken
>
{
    // ...
}


来源:https://stackoverflow.com/questions/48712868/avoid-discriminator-with-aspnetusers-aspnetroles-aspnetuserroles

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!