jmo*_*era 3 entity-framework entity-framework-core asp.net-core
我正在扩展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>();
}
Run Code Online (Sandbox Code Playgroud)
并配置如下:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我一直这样:
"Invalid column name 'Discriminator'.\r\nInvalid column name 'Discriminator'.\r\nInvalid column name 'Discriminator'.\r\nInvalid column name 'Discriminator'."
Run Code Online (Sandbox Code Playgroud)
我知道如果你有派生类,那么你必须在OnModelCreating方法中指定HasDiscriminitor.但IdentityUser,IdentityUserRole和IdentityRole不是抽象类.
我怎么能通过这个?
Iva*_*oev 11
您的上下文是继承IdentityDbContext<TUser>,继而继承IdentityDbContext<TUser, IdentityRole, string>.TUser在这种情况下是你的ApplicationUser,但角色类型是IdentityRole.
因此,基类fluent配置注册IdentityRole为实体.当您将派生的ApplicationRole实体注册时,EF Core会将其视为TPH(每个层次表)继承策略,该策略是使用具有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
>
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1285 次 |
| 最近记录: |