避免使用aspnetusers、aspnetroles和aspnetuserroles的“鉴别器”

8wigbo56  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(414)

我正在扩展 IdentityUser , IdentityUserRole ,和 IdentityRole 这样地:

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不是抽象类。
我该怎么过去?

093gszye

093gszye1#

您的上下文正在继承 IdentityDbContext<TUser> 反过来又继承了 IdentityDbContext<TUser, IdentityRole, string> . TUser 在这种情况下是你的 ApplicationUser ,但角色类型为 IdentityRole .
因此,基类fluent配置寄存器 IdentityRole 作为实体。当您注册派生 ApplicationRole 作为实体,ef-core将其视为tph(tableper-hierarchy)继承策略,该策略是通过具有 Discriminator 列。
要解决这个问题,只需使用适当的基泛型 IdentityDbContext . 因为你也有一个习惯 IdentityUserRole 派生类型,则应使用具有所有泛型类型参数的- 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
>
{
    // ...
}

相关问题