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?
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 string
type.
Hope this helps.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With