When Extend IdentityUserRole and add migration it create the AspNetUserRole table again

Mohammad Arar (MiddleEast) 21 Reputation points
2022-12-22T14:03:01.62+00:00

Iam trying to extend the IdentityUserRole entity but when i add migration it recreating the AspNetUserRoles table with the foreign keys also

my extend entity:

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);  
    }  

the tables already created and working fine but this issue come when i extend IdentityUserRole or IdentityRole same thing

Microsoft Identity Manager
Microsoft Identity Manager
A family of Microsoft products that manage a user's digital identity using identity synchronization, certificate management, and user provisioning.
617 questions
Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
696 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,187 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,016 Reputation points Microsoft Vendor
    2022-12-23T04:04:11.883+00:00

    Hi @Mohammad Arar (MiddleEast) ,

    When Extend IdentityUserRole and add migration it create the AspNetUserRole table again

    I could reproduce the problem, the issue relates the DbContext, after modifying the ApplicationDbContext as below:

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

    The whole 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 });  
            });   
        }  
    }  
    

    You can check the generate migration file from here: 273544-sourcecode.txt

    And the database table as below:

    273561-image.png


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Dillion


1 additional answer

Sort by: Most helpful
  1. Mohammad Arar (MiddleEast) 21 Reputation points
    2022-12-23T11:31:58.74+00:00

    Thank you very much it worked

    0 comments No comments