Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extending ASP.NET Identity Roles: IdentityRole is not part of the model for the current context

I'm trying to use the new ASP.NET Identity in my MVC5 application, specifically I'm trying to integrate ASP.NET Identity into an existing database. I've already read the questions/answers on SO pertaining to DB First and ASP.NET Identity, and having followed all the recommendations I still can't add roles to my database, although I have no problems adding users. Here's my code:

var context = new PayrollDBEntities();
var roleManager = new RoleManager<AspNetRole>(new RoleStore<AspNetRole>(context));

bool roleExists = roleManager.RoleExists(roleDto.Name);
if (roleExists){
    return false;
}

var role = new AspNetRole(roleDto.Name){
    Name = roleDto.Name,
};

IdentityResult result = roleManager.Create(role);//Getting exception here

At the last line of code I get an exception of type 'System.InvalidOperationException': The entity type IdentityRole is not part of the model for the current context.

Here is my context:

public partial class PayrollDBEntities : IdentityDbContext
{
        public PayrollDBEntities()
            : base("name=PayrollDBEntities")
        {
        }

        public virtual DbSet<AspNetRole> AspNetRoles { get; set; }
        public virtual DbSet<AspNetUserClaim> AspNetUserClaims { get; set; }
        public virtual DbSet<AspNetUserLogin> AspNetUserLogins { get; set; }
        public virtual DbSet<AspNetUser> AspNetUsers { get; set; }
......
}

My AspNetUser and AspNetRole classes derive from IdentityUser and IdentityRole respectively, but I'm still getting that exception. Here is my database diagram:

enter image description here

Any help would be greatly appreciated.

like image 870
Mohammad Sepahvand Avatar asked Mar 03 '14 08:03

Mohammad Sepahvand


1 Answers

I know this is an old question, but just in case someone else is having a hard time adding roles/users when they modified asp identity to use numeric primary keys (int/long) instead of the default string for the Identity Roles, so if you have changed the IdentityUserRole in IdentityModels.cs to something like this:

public class Role : IdentityRole<long, UserRole>
{
    public Role() { }
    public Role(string name) { Name = name; }
}

You have to use the class Role instead of the default IdentityRole when constructing the RoleManager, so your code should be like this:

public static void RegisterUserRoles()
{
     ApplicationDbContext context = new ApplicationDbContext();

     var RoleManager = new RoleManager<Role, long>(new RoleStore(context));

     if (!RoleManager.RoleExists("Administrador"))
     {
         var adminRole = new Role {
              Name = "Administrador",
         };
         RoleManager.Create(adminRole);
     }
}

So this should populate your database properly, I think all experienced ASP programmers already know this, but for others this could take some time to figure out.

like image 55
David Ortega Avatar answered Sep 29 '22 22:09

David Ortega