IdentityUserRole Table 列重复且无法删除主键

IdentityUserRole Table column duplicated and can't drop primary key

我的 UserRole 有问题 table 我按以下定义重命名了我的 tables

protected override void OnModelCreating(DbModelBuilder builder)
 {
    builder.Conventions.Remove<PluralizingTableNameConvention>();

    builder.Entity<IdentityUserClaim>().ToTable("UserClaim").HasKey<Int32>(r => r.Id);
    builder.Entity<IdentityUserLogin>().ToTable("UserLogin").HasKey<string>(l => l.UserId);
    builder.Entity<IdentityRole>().ToTable("Role").HasKey<string>(r => r.Id);
    builder.Entity<User>().ToTable("User").HasKey(r => new{ r.IDNumber, r.UserName});
    builder.Entity<IdentityUser>().ToTable("User").HasKey<string>(r => r.UserName);
    builder.Entity<IdentityUserRole>().ToTable("UserRole").HasKey(r => new { r.RoleId, r.UserId });
 }

我没有定义自定义 UserRole class 所以在这种情况下我只是使用 IdentityUserRole。但是,当我查看我的 UserRole table 时,RoleIdIdentityRole_Id 具有相似的值

使用我的 UserManager

 IdentityResult result = await this.UserManager.RemoveFromRoleAsync(regUser.Id, role.Name);

它只删除 IdentityRole_IdIdentityUser_UserName 实体。我想不出删除 RoleIdUserId

的方法

您应该在为 AspNetIdentity

设置关系的代码之前调用 base.OnModelCreating(builder);
protected override void OnModelCreating(DbModelBuilder builder)
{
    // Add this line
    base.OnModelCreating(builder);

    builder.Conventions.Remove<PluralizingTableNameConvention>();

    builder.Entity<IdentityUserClaim>().ToTable("UserClaim").HasKey<Int32>(r => r.Id);
    builder.Entity<IdentityUserLogin>().ToTable("UserLogin").HasKey<string>(l => l.UserId);
    builder.Entity<IdentityRole>().ToTable("Role").HasKey<string>(r => r.Id);
    builder.Entity<User>().ToTable("User").HasKey(r => new{ r.IDNumber, r.UserName});
    builder.Entity<IdentityUser>().ToTable("User").HasKey<string>(r => r.UserName);
    builder.Entity<IdentityUserRole>().ToTable("UserRole").HasKey(r => new { r.RoleId, r.UserId });
}