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

前端 未结 2 638
情话喂你
情话喂你 2021-01-03 15:13

I want to be able to extend the default implementation of IdentityRole to include fields like Description. It\'s easy enough to do this for IdentityUser because IdentityDbCo

2条回答
  •  孤独总比滥情好
    2021-01-03 15:32

    I have just gone through this pain myself. It actually turned out to be pretty simple. Just extend IdentityRole with your new properties.

    public class ApplicationRole : IdentityRole
    {
        public ApplicationRole(string name)
            : base(name)
        { }
    
        public ApplicationRole()
        { }
    
        public string Description { get; set; }
    }
    

    Then you need to add the line

    new public DbSet Roles { get; set; }
    

    into your ApplicationDbContext class like this otherwise you will get errors.

    public class ApplicationDbContext : IdentityDbContext
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {}
    
        new public DbSet Roles { get; set; }
    }
    

    thats all I needed to do. Make sure you change all instances of IdentityRole to ApplicationRole including anything you are seeding. Also, dont forget to issue a "update-database" to apply the changes to your DB. Any existing rows in there won't be seen by your new RoleManager unless you have the "ApplicationRole" set as a discriminator. You can easily add this yourself.

    HTH

    Erik

提交回复
热议问题