【问题标题】:Entity type IdentityRole is not part of the current context实体类型 IdentityRole 不是当前上下文的一部分
【发布时间】:2017-01-11 16:16:15
【问题描述】:

我目前正在尝试将角色添加到我的应用程序中,并且收到此错误“实体类型 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 类

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

【问题讨论】:

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


    【解决方案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) {}     }
    

    【讨论】:

      猜你喜欢
      • 2017-06-01
      • 1970-01-01
      • 2019-01-29
      • 2015-03-22
      • 2013-11-10
      • 2014-11-30
      • 2017-06-19
      • 2011-06-26
      • 2014-01-08
      相关资源
      最近更新 更多