The entity type IdentityRole is not part of the model for the current context (MVC Role Manager)

151 Views Asked by At

I am pretty new to MVC. I am working role manager. Before this, I changed user id datatype to int from string. Now, I would like to have roles in my website. I have created page and added code to run that part. After running website, When I click on Create New User Role, I get this below error in background. Here is my code:

private List<SelectListItem> GetAllRolesAsSelectList()
    {
        List<SelectListItem> SelectRoleListItems =
            new List<SelectListItem>();

        var roleManager =
            new RoleManager<IdentityRole>(
                new RoleStore<IdentityRole>(new ApplicationDbContext()));

        var colRoleSelectList = roleManager.Roles.OrderBy(x => x.Name).ToList();

        SelectRoleListItems.Add(
            new SelectListItem
            {
                Text = "Select",
                Value = "0"
            });

        foreach (var item in colRoleSelectList)
        {
            SelectRoleListItems.Add(
                new SelectListItem
                {
                    Text = item.Name.ToString(),
                    Value = item.Name.ToString()
                });
        }

        return SelectRoleListItems;
    }

This is the line where error is thrown

var roleManager =
                new RoleManager<IdentityRole>(
                    new RoleStore<IdentityRole>(new ApplicationDbContext()));

Here is my IdentityModel page code:

public class CustomUserRole : IdentityUserRole<int> 
{
}
public class CustomUserClaim : IdentityUserClaim<int> 
{ 
}
public class CustomUserLogin : IdentityUserLogin<int> 
{
}
public class CustomRole : IdentityRole<int, CustomUserRole>, IRole<int>
{
    public CustomRole() : base() { }
    public CustomRole(string name) 
        : this()
    { 
        this.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)
    {
    }
} 
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, CustomRole, 
int, CustomUserLogin, CustomUserRole, CustomUserClaim>  
{
    public ApplicationDbContext()
        : base("SMSGoConnection")
    {
    }
    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }

Help would be appreciated.

0

There are 0 best solutions below