扩展 IdentityUserRole 并添加迁移时,它会再次创建 AspNetUserRole 表

Hui Liu-MSFT 48,711 信誉分 Microsoft 外部员工
2024-06-13T07:56:52.3533333+00:00

我正在尝试扩展 IdentityUserRole 实体,但是当我添加迁移时,它还使用外键重新创建 AspNetUserRoles 表

我的扩展实体:

public class ApplicationUserRole: IdentityUserRole<string> { public int EnterpriseId { get; set; } public virtual Enterprise Enterprise { get; set; } public int Deleted { get; set; } }

DbContext Model:

protected override void OnModelCreating(ModelBuilder builder) {


    builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());  

    base.OnModelCreating(builder);  

    builder.Entity<ApplicationUser>(entity =>  
    {  
        entity.ToTable(name: "User");  
    });  

    builder.Entity<IdentityRole>(entity =>  
    {  
        entity.ToTable(name: "Role");  
    });  

    builder.Entity<ApplicationUserRole>(entity =>  
    {  
        entity.ToTable("UserRoles");  
    });  

    builder.Entity<IdentityUserClaim<string>>(entity =>  
    {  
        entity.ToTable("UserClaims");  
    });  

    builder.Entity<IdentityUserLogin<string>>(entity =>  
    {  
        entity.ToTable("UserLogins");  
        //in case you chagned the TKey type  
        //  entity.HasKey(key => new { key.ProviderKey, key.LoginProvider });         
    });  

    builder.Entity<IdentityRoleClaim<string>>(entity =>  
    {  
        entity.ToTable("RoleClaims");  

    });  

    builder.Entity<IdentityUserToken<string>>(entity =>  
    {  
        entity.ToTable("UserTokens");  
        //in case you chagned the TKey type  
        // entity.HasKey(key => new { key.UserId, key.LoginProvider, key.Name });  
    });  

    builder.ConfigureEntity();  

}  

migration file:


protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn<int>( name: "Deleted", table: "UserRoles", type: "int", nullable: false, defaultValue: 0);

        migrationBuilder.AddColumn<int>(  
            name: "EnterpriseId",  
            table: "UserRoles",  
            type: "int",  
            nullable: false,  
            defaultValue: 0);  

        migrationBuilder.CreateTable(  
            name: "AspNetUserRoles",  
            columns: table => new  
            {  
                UserId = table.Column<string>(type: "nvarchar(450)", nullable: false),  
                RoleId = table.Column<string>(type: "nvarchar(450)", nullable: false)  
            },  
            constraints: table =>  
            {  
                table.PrimaryKey("PK_AspNetUserRoles", x => new { x.UserId, x.RoleId });  
                table.ForeignKey(  
                    name: "FK_AspNetUserRoles_Role_RoleId",  
                    column: x => x.RoleId,  
                    principalTable: "Role",  
                    principalColumn: "Id",  
                    onDelete: ReferentialAction.Cascade);  
                table.ForeignKey(  
                    name: "FK_AspNetUserRoles_User_UserId",  
                    column: x => x.UserId,  
                    principalTable: "User",  
                    principalColumn: "Id",  
                    onDelete: ReferentialAction.Cascade);  
            });  

        migrationBuilder.CreateIndex(  
            name: "IX_UserRoles_EnterpriseId",  
            table: "UserRoles",  
            column: "EnterpriseId");  

        migrationBuilder.CreateIndex(  
            name: "IX_AspNetUserRoles_RoleId",  
            table: "AspNetUserRoles",  
            column: "RoleId");  

        migrationBuilder.AddForeignKey(  
            name: "FK_UserRoles_AspNetUserRoles_UserId_RoleId",  
            table: "UserRoles",  
            columns: new[] { "UserId", "RoleId" },  
            principalTable: "AspNetUserRoles",  
            principalColumns: new[] { "UserId", "RoleId" });  

        migrationBuilder.AddForeignKey(  
            name: "FK_UserRoles_Enterprises_EnterpriseId",  
            table: "UserRoles",  
            column: "EnterpriseId",  
            principalTable: "Enterprises",  
            principalColumn: "Id",  
            onDelete: ReferentialAction.Cascade);  
    }  

表已经创建并且工作正常,但是当我扩展 IdentityUserRole 或 IdentityRole 时会出现此问题

Note:此问题总结整理于:When Extend IdentityUserRole and add migration it create the AspNetUserRole table again

开发人员技术 | .NET | Entity Framework Core
0 个注释 无注释
{count} 票

1 个答案

排序依据: 非常有帮助
  1. 匿名
    2024-06-13T08:39:14.0933333+00:00

    扩展 IdentityUserRole 并添加迁移时,它会再次创建 AspNetUserRole 表

    在修改ApplicationDbContext后,我可以重现该问题,该问题与DbContext有关,如下所示:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole, string, IdentityUserClaim<string>, ApplicationUserRole, IdentityUserLogin<string>, IdentityRoleClaim<string>, IdentityUserToken<string>>  
    {  
    

    整个 ApplicationDbContext:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser, IdentityRole, string, IdentityUserClaim<string>, ApplicationUserRole, IdentityUserLogin<string>, IdentityRoleClaim<string>, IdentityUserToken<string>>  
    {  
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)  
            : base(options)  
        {  
        }  
        protected override void OnModelCreating(ModelBuilder builder)  
        {  
    
            builder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly());  
            base.OnModelCreating(builder);  
            builder.Entity<ApplicationUser>(entity =>  
            {  
                entity.ToTable(name: "User");   
    
            });  
            builder.Entity<IdentityRole>(entity =>  
            {  
                entity.ToTable(name: "Role");  
            });  
            builder.Entity<ApplicationUserRole>(entity =>  
            {  
                entity.ToTable(name: "UserRoles"); ;  
            });  
            builder.Entity<IdentityUserClaim<string>>(entity =>  
            {  
                entity.ToTable("UserClaims");  
            });  
            builder.Entity<IdentityUserLogin<string>>(entity =>  
            {  
                entity.ToTable("UserLogins");  
                //in case you chagned the TKey type  
                //  entity.HasKey(key => new { key.ProviderKey, key.LoginProvider });         
            });  
            builder.Entity<IdentityRoleClaim<string>>(entity =>  
            {  
                entity.ToTable("RoleClaims");  
            });  
            builder.Entity<IdentityUserToken<string>>(entity =>  
            {  
                entity.ToTable("UserTokens");  
                //in case you chagned the TKey type  
                // entity.HasKey(key => new { key.UserId, key.LoginProvider, key.Name });  
            });   
        }  
    }  
    

    您可以从此处查看生成的迁移文件:

    数据库表如下:

    273561-image.png


    如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。

    注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。

    0 个注释 无注释

你的答案

提问者可以将答案标记为“已接受”,版主可以将答案标记为“已推荐”,这有助于用户了解答案是否解决了提问者的问题。