0

In my MVC5 project, I have exteneded the IdentityUser class in my DbContext with the following implementation:

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext() : base("DefaultConnection") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<IdentityUser>() .ToTable("Users"); modelBuilder.Entity<ApplicationUser>() .ToTable("Users"); } } 

Using an ApplicationUser class:

public class ApplicationUser : IdentityUser { public string EmailAddress { get; set; } public string Name { get; set; } public DateTime JoinDate { get; set; } public string ConfirmationToken { get; set; } public bool EmailIsConfirmed { get; set; } } 

However, whenever I try to do the same thing and extend the IdentityRole class it is not working. If I simply create an ApplicationRole class that inherits from IdentityRole (and change all instances of IdentityRole in my MVC project to ApplicationRole), then no roles are returned from the database. For example, using the following

RoleManager<ApplicationRole> RoleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(new ApplicationDbContext()));

yields a result count of 0 when I call RoleManager.Roles.

Has anyone done this yet and could provide some guidance?

UPDATE I had forgotten to change my DB Seed method to use my new ApplicationRole - it was still using IdentityRole. After doing so, I couldn't apply an update to my database because I kept getting a DBValidation error. After adapting some of the code from this post, I was able to get everything working by overriding the ValidateEntity method of the DbContext

1 Answer 1

3

How to Extend Microsoft.AspNet.Identity.EntityFramework.IdentityRole

  1. Make sure you are inheriting from IdentityRole for your ApplicationRole
  2. Make sure you add following in your ApplicationDbContext, so that you work directly with your ApplicationRole.

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

  3. You following the steps as follow

    • Enable-Migration
    • Add-Migration "InitialSetup"
    • Update-Database
    • Modify any model properties
    • Add-Migration "ModificationName"
    • Update-Database

Your code to create RoleManager is good now.

RoleManager<ApplicationRole> roleManager = new RoleManager<ApplicationRole>(new RoleStore<ApplicationRole>(new ApplicationDbContext())); ApplicationRole role = roleManager.FindByName("Admin"); 
  • RoleManager<ApplicationRole> do not have any property with name "Roles"
  • ApplicationDbContext has a property named "Roles". As you are followed Step 2, this property now returned as DbSet<ApplicationRole>
Sign up to request clarification or add additional context in comments.

2 Comments

I have tried this exact same thing, but I always got a DbValidation error when I tried to update the database. I had to override the ValidateEntity method of my DbContext - I'll update my question soon
It is good you could solve your issue. Moreover, I just added the code to update the role.Description to database, it worked.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.