在将ASP.NET Core 1.1项目更新到.NET Core 2.1版本期间,我创建了以下类:
public class IdentityUserCustom : IdentityUser
{
public IdentityUserCustom() : base()
{ }
public virtual ICollection<IdentityUserClaim<string>> Claims { get; }
public virtual ICollection<ApplicationUserRole> UserRoles { get; }
public virtual ICollection<IdentityUserLogin<string>> Logins { get; }
}
public class ApplicationUser : IdentityUserCustom
{ ... }
public class ApplicationUserRole : IdentityUserRole<string>
{
public virtual ApplicationUser User { get; set; }
public virtual ApplicationRole Role { get; set; }
}
public class ApplicationRole : IdentityRole<string>
{
public ApplicationRole() : base() { }
public ApplicationRole(string roleName) : base(roleName) { }
public virtual ICollection<MyEntity> MyEntities { get; set; }
}
public class MyEntity
{
public string RoleID { get; set; }
...
[ForeignKey("RoleID")]
public virtual ApplicationRole Role { get; set; }
...
}
但是当我创建初始迁移时,我发现它想要创建2
角色表:标准AspNetRoles
和ApplicationRole
:
migrationBuilder.CreateTable(
name: "ApplicationRole",
columns: table => new
{
Id = table.Column<string>(nullable: false),
Name = table.Column<string>(nullable: true),
NormalizedName = table.Column<string>(nullable: true),
ConcurrencyStamp = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_ApplicationRole", x => x.Id);
});
migrationBuilder.CreateTable(
name: "AspNetRoles",
columns: table => new
{
Id = table.Column<string>(nullable: false),
Name = table.Column<string>(maxLength: 256, nullable: true),
NormalizedName = table.Column<string>(maxLength: 256, nullable: true),
ConcurrencyStamp = table.Column<string>(nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_AspNetRoles", x => x.Id);
});
所有数据库文件(实体,上下文,映射,迁移...)都放在单独的项目中,Startup.cs中IdentityUser
和IdentityRole
的所有表示都替换为{{1 }和ApplicationUser
。
当我尝试为ApplicationRole
实体设置表名时(例如,通过ApplicationRole
属性),在创建迁移期间会出现错误:
[Table("AspNetRoles")]
如何告诉EF Core 2.1,Cannot use table 'AspNetRoles' for entity type 'IdentityRole' since it is being used for entity type 'ApplicationRole' and there is no relationship between their primary keys.
是否相同ApplicationRole
并且它应该转换为相同的IdentityRole
表?