Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extend IdentityRole into a custom table name

I am trying to implement role based security to my MVC 5, Identity 2.0 project. I have been looking at some tutorials on the internet but am a bit stuck.

I have created the following:

public class Role : IdentityRole
{
    public Role(string name) : base(name)
    { }

    public Role()
    { }

    public string Description { get; set; }
    public string MenuIcon { get; set; }
    public string ControllerName { get; set; }
    public string ActionName { get; set; }
}

and my context:

    new public DbSet<Role> Roles { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        base.OnModelCreating(modelBuilder);

        //rename the tables and columns used by the Identity Framework
        modelBuilder.Entity<User>().ToTable("User");
        modelBuilder.Entity<Role>().ToTable("Role");
        modelBuilder.Entity<UserRole>().ToTable("UserRole");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaim");
    }

and my Seed method:

    RoleManager roleManager = new RoleManager(new RoleStore<Role>(context));
    roleManager.Create<Role, string>(new Role("Orders"));

However when I run the application, it creates the database with 2 tables for the Roles: Role (which is where I want the roles to exist and it contains my custom properties) and AspNetRoles. It does populate both tables with the role that I created in my Seed method.

What am I doing wrong exactly that it is creating 2 tables?

like image 938
Lock Avatar asked Oct 02 '14 09:10

Lock


2 Answers

All asp.net tables should recognize new table names not only the changed table.

User table:

public class User : IdentityUser<string, UserLogin, UserRole, UserClaim>
{
    // Your properties
}

UserLogin table:

public class UserLogin : IdentityUserLogin { }

UserRole table:

public class UserRole : IdentityUserRole { }

UserClaim table:

public class UserClaim : IdentityUserClaim { }

Role table: This hat to be inherited from IdentityRole<string, UserRole>

public class Role : IdentityRole<string, UserRole> 
{ 
    //Any relevant properties

    public string Description { get; set; }
    public string MenuIcon { get; set; }
    public string ControllerName { get; set; }
    public string ActionName { get; set; }    
}

Data context: This has to be inherited from IdentityDbContext<User, Role, string, UserLogin, UserRole, UserClaim>

public class YourDataContext : IdentityDbContext<User, Role, string,   UserLogin, UserRole, UserClaim>
{
    // Your DbSet items

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

        modelBuilder.Entity<User>().ToTable("User");
        modelBuilder.Entity<Role>().ToTable("Role");
        modelBuilder.Entity<UserClaim>().ToTable("UserClaim");
        modelBuilder.Entity<UserLogin>().ToTable("UserLogin");
        modelBuilder.Entity<UserRole>().ToTable("UserRole");

        modelBuilder.Entity<User>().Property(r => r.Id);
        modelBuilder.Entity<UserClaim>().Property(r => r.Id);
        modelBuilder.Entity<Role>().Property(r => r.Id);

       //Add your new properties of Role table in here.


    }
}

In this example I have used Id of the tables as stringtype.

Hope this helps.

like image 84
DSR Avatar answered Oct 31 '22 21:10

DSR


Delete this line:

new public DbSet<Role> Roles { get; set; }

Then you have to replace the default AspNetRoles table columns by your own columns.

var applicationRole = modelBuilder.Entity<ApplicationRole>().ToTable("Roles");
applicationRole.Property(r => r.Name).HasColumnName("Description");
...

However if you want to customize your AspNet Identity profile information could be better to create new related tables (check this link).

like image 2
Xavier Egea Avatar answered Oct 31 '22 20:10

Xavier Egea