本文介绍了使用 AspNetUsers、AspNetRoles 和 & 避免“鉴别器"AspNet 用户角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我像这样扩展 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'.
Invalid column name 'Discriminator'.
Invalid column name 'Discriminator'.
Invalid 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.

我怎样才能克服这个问题?

How can I get past this?

推荐答案

您的上下文正在继承 IdentityDbContext 依次继承 IdentityDbContext代码>.TUser 在这种情况下是您的 ApplicationUser,但角色类型是 IdentityRole.

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.

因此基类 fluent 配置将 IdentityRole 注册为 entity.当您将派生的 ApplicationRole 注册为实体时,EF Core 将其视为 TPH (Table Per Hierarchy) 继承策略,它是用具有 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 派生类型,您应该使用具有所有泛型类型参数的类型 - IdentityDbContext:

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; 避免“鉴别器"AspNet 用户角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 01:52