C# 实体类型IdentityRole不是当前上下文的一部分
我目前正试图将角色添加到我的应用程序中,我收到了一个错误“EntityTypeIdentityRole不是当前上下文的一部分”。我以前也有同样的错误,除了AppRole,但我通过在AppIdentityDdbContext类中包含IdentityDdbContext之后的所有参数解决了这个问题,但是我无法修复这个问题,谢谢您的帮助 这是我得到错误信息的线路C# 实体类型IdentityRole不是当前上下文的一部分,c#,asp.net-mvc,entity-framework,asp.net-identity,C#,Asp.net Mvc,Entity Framework,Asp.net Identity,我目前正试图将角色添加到我的应用程序中,我收到了一个错误“EntityTypeIdentityRole不是当前上下文的一部分”。我以前也有同样的错误,除了AppRole,但我通过在AppIdentityDdbContext类中包含IdentityDdbContext之后的所有参数解决了这个问题,但是我无法修复这个问题,谢谢您的帮助 这是我得到错误信息的线路 ClaimsIdentity ident = await UserManager.CreateIdentityAsync(user,Defa
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
}
}
问题在于我显式声明了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) {} }
公共类AppUserStore:UserStore、IUserStore、IUserStore、IDisposable其中TUser:Microsoft.AspNet.Identity.EntityFramework.IdentityUser{
公共AppUserStore(DbContext上下文):基(上下文){}
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;
}
}
public class AppRole:IdentityRole
{
public AppRole():base() { }
public AppRole(string name) : base (name)
{
}
}
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) {} }