避免与AspNetUsers,AspNetRoles和& AspNetUserRoles [英] Avoid 'Discriminator' with AspNetUsers, AspNetRoles, & AspNetUserRoles

查看:82
本文介绍了避免与AspNetUsers,AspNetRoles和& AspNetUserRoles的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像这样扩展IdentityUserIdentityUserRoleIdentityRole:

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>();
    }

,配置如下:

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);
    }
}

我不断得到这个:

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

我知道如果您有派生类,则必须在OnModelCreating方法中指定HasDiscriminitor.但是IdentityUser,IdentityUserRole和IdentityRole并不是抽象类.

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.

我该如何克服?

推荐答案

您的上下文继承了

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.

因此,基类的流利配置将IdentityRole注册为实体.当您将派生的ApplicationRole注册为实体时,EF Core会将其视为 TPH(每个层次的表)继承策略,该策略是通过具有Discriminator列的单个表来实现的.

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.

要解决此问题,只需使用适当的基本通用IdentityDbContext.由于还具有自定义的IdentityUserRole派生类型,因此应将其与所有通用类型参数一起使用-

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
>
{
    // ...
}

这篇关于避免与AspNetUsers,AspNetRoles和&amp; AspNetUserRoles的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆