【发布时间】:2018-07-20 15:20:35
【问题描述】:
我正在扩展 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 不是抽象类。
我怎样才能克服这个问题?
【问题讨论】:
-
你能显示你的上下文的基类定义吗,例如
public class ApplicationDbContext : IdentityDbContext<???> -
@IvanStoev 请查看更新的代码。
-
您没有注册其他类型。
IdentityDbContext也存在多个类型参数 -
由于使用自定义角色,继承自
IdentityDbContext<ApplicationUser, ApplicationRole, string>
标签: entity-framework asp.net-core entity-framework-core