How to extend IdentityRole to a custom table name

I am trying to implement role-based protection for my MVC 5, Identity 2.0 project. I watched a few lessons on the Internet, but got a little stuck.

I 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 launch the application, it creates a database with 2 tables for Roles: Role(where I want the roles to exist and it has my user properties) and AspNetRoles. It fills both tables with the role that I created in the Seed method.

What am I doing wrong that it creates 2 tables?

+4
source share
2 answers

asp.net .

:

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

UserLogin:

public class UserLogin : IdentityUserLogin { }

UserRole:

public class UserRole : IdentityUserRole { }

UserClaim:

public class UserClaim : IdentityUserClaim { }

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

: 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.


    }
}

string.

, .

+3

:

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

AspNetRoles .

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

, AspNet, ( ).

+2

All Articles