简体   繁体   中英

Issues with extending Asp.Net Identity IdentityRole

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM