I\'m developing a Multi-tenancy web application with ASP.Net MVC and Identity 2.0. I have extended the IdentityRole like this:
public class ApplicationRole :
Simply alter database's schema on ApplicationDbContext
's OnModelCreating
method like this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
var role = modelBuilder.Entity<IdentityRole>()
.ToTable("AspNetRoles");
role.Property(r => r.Name)
.IsRequired()
.HasMaxLength(256)
.HasColumnAnnotation("Index", new IndexAnnotation(
new IndexAttribute("RoleNameIndex")
{ IsUnique = false }));
}
But you must customize RoleValidator
class also. Because default role validator invalidates duplicated role names.
public class MyRoleValidator:RoleValidator<ApplicationRole>
{
public override async Task<IdentityResult> ValidateAsync(ApplicationRole item)
{
// implement your validation logic here
return IdentityResult.Success;
}
}
Now every time you create the role manager you must set the role validator.
roleManager.RoleValidator=new MyRoleValidator();