我正在扩展 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不是抽象类。
我该怎么过去?
1条答案
按热度按时间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>
: