【问题标题】:Avoid 'Discriminator' with AspNetUsers, AspNetRoles, & AspNetUserRoles避免使用 AspNetUsers、AspNetRoles 和 AspNetUserRoles 的“鉴别器”
【发布时间】:2018-07-20 15:20:35
【问题描述】:

我正在扩展 IdentityUserIdentityUserRoleIdentityRole,如下所示:

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 不是抽象类。

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

【问题讨论】:

  • 你能显示你的上下文的基类定义吗,例如public class ApplicationDbContext : IdentityDbContext&lt;???&gt;
  • @IvanStoev 请查看更新的代码。
  • 您没有注册其他类型。 IdentityDbContext 也存在多个类型参数
  • 由于使用自定义角色,继承自IdentityDbContext&lt;ApplicationUser, ApplicationRole, string&gt;

标签: entity-framework asp.net-core entity-framework-core


【解决方案1】:

您的上下文继承了IdentityDbContext&lt;TUser&gt;,而后者又继承了IdentityDbContext&lt;TUser, IdentityRole, string&gt;TUser 在这种情况下是您的 ApplicationUser,但角色类型是 IdentityRole

因此,基类流式配置将IdentityRole 注册为实体。当您将派生的ApplicationRole 注册为实体时,EF Core 将其视为TPH (Table Per Hierarchy) Inheritance Strategy,这是通过具有Discriminator 列的单个表实现的。

要解决此问题,只需使用正确的基本通用 IdentityDbContext。由于您还有一个自定义的 IdentityUserRole 派生类型,因此您应该使用带有所有泛型类型参数的那个 - IdentityDbContext&lt;TUser,TRole,TKey,TUserClaim,TUserRole,TUserLogin,TRoleClaim,TUserToken&gt;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-22
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    相关资源
    最近更新 更多