Search code examples
asp.net-identity-2

Customizing asp.net identityrole is creating additional table in migration script


I am trying to customize identityRole table to add a new property , but it is creating a new table script instead of creating the property in the script . How can I generate the script which uses the db.tb_appRoles table instead of both db.aspnetRoles and db.tb_appRoles ?

My configuration is as below :

public class AppRole : IdentityRole
    {
        public string Description { get; set; }
    }

 public class AppRoleMapping : EntityTypeConfiguration<AppRole>
    {
        public AppRoleMapping()
            : base()
        {
            Map(m =>
            {
                m.ToTable("tb_AppRole");
            }).HasKey(e => e.Id); ;
        }

    }

// Generated script with two separate tables

public partial class initial : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.AspNetRoles",
            c => new
                {
                    Id = c.String(nullable: false, maxLength: 128),
                    Name = c.String(nullable: false, maxLength: 256),
                })
            .PrimaryKey(t => t.Id)
            .Index(t => t.Name, unique: true, name: "RoleNameIndex");

        CreateTable(
            "dbo.tb_AppRole",
            c => new
                {
                    Id = c.String(nullable: false, maxLength: 128),
                    Description = c.String(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.AspNetRoles", t => t.Id)
            .Index(t => t.Id);

    }

PS : This is working as expected in case of AppUser inherited from IdentityUser . Here is the script generated for IdentityUser :

public class AppUser : IdentityUser
{
    public string Description { get; set; }
}

public class AppUserMapping : EntityTypeConfiguration<AppUser>
{
    public AppUserMapping():base()
    {
        this.Map(m =>
        {
        }).HasKey(e => e.Id);

    }

}

public partial class initial : DbMigration
    {
        public override void Up()
        {

 CreateTable(
                "dbo.tb_AppUser",
                c => new
                    {
                        Id = c.String(nullable: false, maxLength: 128),
                        Description = c.String(),
                        Email = c.String(),
                        EmailConfirmed = c.Boolean(nullable: false),
                        PasswordHash = c.String(),
                        SecurityStamp = c.String(),
                        PhoneNumber = c.String(),
                        PhoneNumberConfirmed = c.Boolean(nullable: false),
                        TwoFactorEnabled = c.Boolean(nullable: false),
                        LockoutEndDateUtc = c.DateTime(),
                        LockoutEnabled = c.Boolean(nullable: false),
                        AccessFailedCount = c.Int(nullable: false),
                        UserName = c.String(),
                    })
                .PrimaryKey(t => t.Id);
}

Solution

  • The reason this behavior is seen is because I was using AppUser inherited from IdentityUser default .

    The fix for this is to use

    AppUSer :IdentityUser<int,AppRole,AppUserRole,AppUserLogis,AppUserClaim>{
    
    }
    

    where AppRole,AppUserRole,AppUserLogis,AppUserClaim are the customized entitities for IdentityRole, IdentityUserRole,IdentityUserLogin,IdentityUserClaim .

    Also same has to be reflected in the IdentityDBContext { }