实体类型IdentityRole不是当前上下文的一部分

时间:2016-09-03 20:44:56

标签: c# asp.net-mvc entity-framework asp.net-identity

我目前正在尝试向我的应用添加角色,而且我收到此错误"实体类型IdentityRole不是当前上下文的一部分"。我以前有同样的错误,除了AppRole,但我通过在AppIdentityDbContext类中包含IdentityDbContext之后的所有参数解决了这个问题,但我无法解决这个问题,谢谢你的帮助

这是我在

上收到错误的行
ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user,DefaultAuthenticationTypes.ApplicationCookie);

DbContext类

 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类

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


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

1 个答案:

答案 0 :(得分:1)

问题在于我已经明确声明了IdentityRole的UserStore,我创建了另一个从UserStore扩展的类,解决了这个问题。

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) {}     }