Search code examples
c#asp.net-mvcentity-frameworkasp.net-identity

Entity type IdentityRole is not part of the current context


I'm currently trying to add roles to my application and I'am getting this error "Entity type IdentityRole is not part of the current context". I previously had the same error except with AppRole but I resolved that issue by including all the parameters after the IdentityDbContext in the AppIdentityDbContext class but I cannot fix this one,thanks for the help

This is the line I get the error on

ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user,DefaultAuthenticationTypes.ApplicationCookie);

DbContext Class

 public class AppIdentityDbContext : IdentityDbContext<User, AppRole, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>
    {
        public AppIdentityDbContext() : base("ProGP_Db") { }

        static AppIdentityDbContext()
        {
            Database.SetInitializer<AppIdentityDbContext>(new IdentityDbInit());
        }
        public static AppIdentityDbContext Create()
        {
            return new AppIdentityDbContext();
        }
    }
    public class IdentityDbInit
    : DropCreateDatabaseIfModelChanges<AppIdentityDbContext>
    {
        protected override void Seed(AppIdentityDbContext context)
        {
            PerformInitialSetup(context);
            base.Seed(context);
        }
        public void PerformInitialSetup(AppIdentityDbContext context)
        {
            // initial configuration will go here
        }
    }

AppUserManager Class

public class AppUserManager : UserManager<User,string>
{

    public AppUserManager(IUserStore<User> store): base(store){}


    public static AppUserManager Create(IdentityFactoryOptions<AppUserManager> options,IOwinContext context)
    {
        AppIdentityDbContext db = context.Get<AppIdentityDbContext>();
        AppUserManager manager = new AppUserManager(new UserStore<User>(db));

        manager.PasswordValidator = new PasswordValidator
        {
            RequiredLength = 6,
            RequireNonLetterOrDigit = false,
            RequireDigit = false,
            RequireLowercase = true,
            RequireUppercase = true
        };


        return manager;
    }
}

AppRoleManager

public class AppRoleManager : RoleManager<AppRole>,IDisposable
{
    public AppRoleManager(RoleStore<AppRole> store):base(store)
    { }


    public static AppRoleManager Create(IdentityFactoryOptions<AppRoleManager> options, IOwinContext context)
    {
        //AppIdentityDbContext db = context.Get<AppIdentityDbContext>();
        //AppRoleManager manager = new AppRoleManager(new RoleStore<AppRole>(db));
        return new AppRoleManager(new RoleStore<AppRole>(context.Get<AppIdentityDbContext>()));

        //return manager;
    }
}

AppRole class

  public class AppRole:IdentityRole
{
    public AppRole():base() { }


    public AppRole(string name) : base (name)
    {
    }
}

Solution

  • The problem was with the UserStore I had IdentityRole explicitly declared,I created another class extending from UserStore which solved the issue.

    public class AppUserStore<TUser> : UserStore<TUser, Role, string, IdentityUserLogin, IdentityUserRole, IdentityUserClaim>, IUserStore<TUser>, IUserStore<TUser, string>, IDisposable where TUser : Microsoft.AspNet.Identity.EntityFramework.IdentityUser {
    
    public AppUserStore(DbContext context) :base(context) {}     }