EF Core 2.1代码首先为cutom Role实体创建重复的表

时间:2018-06-12 16:31:31

标签: c# ef-migrations asp.net-core-2.1 ef-core-2.1

在将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 角色表:标准AspNetRolesApplicationRole

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中IdentityUserIdentityRole的所有表示都替换为{{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表?

0 个答案:

没有答案