user2779312
user2779312

Reputation: 661

ASP Identity - The entity type IdentityRole is not part of the model for the current context

I extended the Identity Models, so that I can have an Int instead of a string regarding the users Primary Key.

namespace BeyondMembership.Models
{
    // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
    public class ApplicationUser : IdentityUser<int, CustomUserLogin, CustomUserRole, CustomUserClaim>
    {
      ...
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole,
        int, CustomUserLogin, CustomUserRole, CustomUserClaim>
    {
        public ApplicationDbContext()
            : base("DefaultConnection")
        {

        }

        static ApplicationDbContext()
        {
            // Set the database intializer which is run once during application start
            // This seeds the database with admin user credentials and admin role
            Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }
}

public class CustomUserRole : IdentityUserRole<int> { }
public class CustomUserClaim : IdentityUserClaim<int> { }
public class CustomUserLogin : IdentityUserLogin<int> { }

public class CustomRole : IdentityRole<int, CustomUserRole>
{
    public CustomRole() { }
    public CustomRole(string name) { Name = name; }
}

public class CustomUserStore : UserStore<ApplicationUser, CustomRole, int,
    CustomUserLogin, CustomUserRole, CustomUserClaim>
{
    public CustomUserStore(ApplicationDbContext context)
        : base(context)
    {
    }
}

public class CustomRoleStore : RoleStore<CustomRole, int, CustomUserRole>
{
    public CustomRoleStore(ApplicationDbContext context)
        : base(context)
    {
    }
}

So I am getting an error on roleManager.FindByName(roleName);:

//Create [email protected] with password=Admin@123456 in the Admin role        
public static void InitializeIdentityForEF(ApplicationDbContext db)
{
    var userManager = HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
    var roleManager = HttpContext.Current.GetOwinContext().Get<ApplicationRoleManager>();
    var role = roleManager.FindByName(roleName); // The entity type IdentityRole is not part of the model for the current context

Thanks a lot for your help.

Upvotes: 0

Views: 2654

Answers (2)

Senthil Kumaran
Senthil Kumaran

Reputation: 51

Since you used CustomRole - you should apply all other places too, hope you get the point.

public class ApplicationRoleManager : RoleManager<CustomRole,int>
{
    public ApplicationRoleManager(IRoleStore<CustomRole,int> roleStore)
        : base(roleStore)
    {
    }

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        //return new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
        return new ApplicationRoleManager(new CustomRoleStore(context.Get<ApplicationDbContext>()));
    }
}

Upvotes: 1

Hugo Hil&#225;rio
Hugo Hil&#225;rio

Reputation: 2908

You are probably missing some implementations. Take a look at this sample, it's already using an Int as Primary key, and doing what you need to do.

https://github.com/TypecastException/AspNet-Identity-2-With-Integer-Keys

Upvotes: 1

Related Questions