2016-09-03 4 views
0

В настоящее время я пытаюсь добавить роли в мое приложение, и я получаю эту ошибку «Тип объекта IdentityRole не является частью текущего контекста». Я ранее имел ту же ошибку, кроме как с AppRole, но я решил эту проблему, включив все параметры после IdentityDbContext в класс AppIdentityDbContext, но я не могу исправить это, спасибо за помощь.Тип объекта IdentityRole не является частью текущего контекста

Это строка, в которой я получаю сообщение об ошибке

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

ответ

0

Проблема была с UserStore я был IdentityRole явно объявлен, я создал еще один класс, расширяющий от 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) {}  } 
Смежные вопросы