Asp.Net Identity 2.0自定义角色管理器:实体类型IdentityRole不是当前上下文的模型的一部分

我已在WebForms ASP.NET应用程序中自定义RoleManager,以便在Roles表中集成新字段。

我按照这里给出的说明进行了操作: http : //johnatten.com/2014/06/22/asp-net-identity-2-0-customizing-users-and-roles/

所以在identitymodels.cs中我添加了:

public class ApplicationRole : IdentityRole { public ApplicationRole() : base() { } public ApplicationRole(string name) : base(name) { } public int Level{ get; set; } //this is the new field in the Roles table } 

并修改了OnModelCreating方法:

  protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); // This needs to go before the other rules! modelBuilder.Entity().ToTable("Users").Property(p => p.Id).HasColumnName("UserId"); modelBuilder.Entity().ToTable("UserRoles"); modelBuilder.Entity().ToTable("UserLogins"); modelBuilder.Entity().ToTable("UserClaims").Property(p => p.Id).HasColumnName("UserClaimId"); modelBuilder.Entity().ToTable("Roles").Property(p => p.Id).HasColumnName("RoleId"); modelBuilder.Ignore(); modelBuilder.Ignore(); } 

然后在IdentityConfig.cs文件中我添加了:

  public class ApplicationRoleManager : RoleManager { public ApplicationRoleManager( IRoleStore roleStore) : base(roleStore) { } //I'm not really sure about the following (IOwinContext??): public static ApplicationRoleManager Create( IdentityFactoryOptions options, Microsoft.Owin.IOwinContext context) { return new ApplicationRoleManager( new RoleStore(context.Get())); } } 

所以现在尝试创建一个新角色:

 var roleManager = new RoleManager(new RoleStore(new ApplicationDbContext())); //the above line differs from what is described in the above blog post. ApplicationRole existingRole = roleManager.FindByName(newRoleName); //ERROR!! if(existingRole==null) { var role = new ApplicationRole(); role.Name = newRoleName; int grade = 0; if(int.TryParse(TxtGrade.Text,out grade)) { role.Level= level; } roleManager.Create(role); } 

我收到以下错误:

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

在堆栈下面:

 System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType) +208 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +51 System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +137 System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +38 System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +98 System.Linq.Queryable.Any(IQueryable`1 source, Expression`1 predicate) +84 Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext`6.ValidateEntity(DbEntityEntry entityEntry, IDictionary`2 items) +4106 System.Data.Entity.DbContext.GetValidationErrors() +290 System.Data.Entity.Internal.InternalContext.SaveChangesAsync(CancellationToken cancellationToken) +151 System.Data.Entity.Internal.LazyInternalContext.SaveChangesAsync(CancellationToken cancellationToken) +75 System.Data.Entity.DbContext.SaveChangesAsync(CancellationToken cancellationToken) +60 System.Data.Entity.DbContext.SaveChangesAsync() +63 Microsoft.AspNet.Identity.EntityFramework.d__2.MoveNext() +215 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter.GetResult() +26 Microsoft.AspNet.Identity.CultureAwaiter.GetResult() +63 Microsoft.AspNet.Identity.d__0.MoveNext() +925 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +92 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +58 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +28 Microsoft.AspNet.Identity.AsyncHelper.RunSync(Func`1 func) +409 Microsoft.AspNet.Identity.RoleManagerExtensions.Create(RoleManager`2 manager, TRole role) +245 

关于方法roleManager.Create(role);

请注意,roleManager不为null,整个身份validation工作正常。

似乎我错过了IdentityRole上的一些更改,它仍然由create方法调用。

我该如何解决这个错误? 我错过了改变什么?