Search code examples
asp.net-core-mvcidentityserver4

asp.net mvc adding custom column to identity role table


This is how I add new column to Identity role table

 public class ApplicationRole : IdentityRole
{
    public ApplicationRole() : base() { }
    public ApplicationRole(string name, long customId) : base(name)
    {
        this.CustomId= customId;
    }
    public virtual long CustomId{ get; set; }
}

In dbContext , I added

 modelBuilder.Entity<ApplicationRole>().ToTable("AspNetRoles");

using this , new column CustomId is added successfully to AspNetRoles table using migration .
But when I have to call role table like

private readonly IdentityDbContext db_Identity;
..
..
 db_Identity.Roles.Select(x=>x.CustomId) 

I can't find newly added column CustomId here .
Is there any step I missed ?

Update

With @Chirs's answer , I can get CoustomId , but when I run , I got this error

Cannot create a DbSet for 'IdentityRole' because this type is not included in the model for the context

Solution

  • If you are using custom Identity models, you also need to inherit from one of the generic IdentityDbContext classes and specify your custom type in the appropriate type param. In this case, you just need something like:

    public class MyDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string>
    

    Then use your custom context class:

    private readonly MyDbContext db_Identity;