0

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);
}
danludwig
  • 46,965
  • 25
  • 159
  • 237
Chandan
  • 1,486
  • 2
  • 15
  • 24

2 Answers2

0

override OnModelCreating of your Db context class to bind your AppRole table to aspnetRoles.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<AppRole>().ToTable("aspnetRoles");

    // Commente below line if you got any errors.
    modelBuilder.Entity<AppRole>().Property(r => r.Id);

}

Hope this helps.

DSR
  • 4,588
  • 29
  • 28
0

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 { }

Chandan
  • 1,486
  • 2
  • 15
  • 24