我在应用程序中使用ASP.NET Identity时出错。多个对象集不支持按类型。对象设置"ApplicationRoles"one_answers"角色"都可以包含类型为的实例'XXX.Identity.ApplicationRole'.
我在stackoverflow中看到了一些关于此错误的问题。所有这些都指示在两个相同类型的DbSet对象上。但在我的DbContext中,没有相同类型的DbSet。
当我想启用自动迁移时引发异常
我的上下文如下:
public class XXXDbContext :
IdentityDbContext<ApplicationUser, ApplicationRole, int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
public override IDbSet<ApplicationUser> Users { get; set; }
public DbSet<ApplicationUserLogin> ApplicationUserLogins { get; set; }
public DbSet<ApplicationUserRole> ApplicationUserRoles { get; set; }
public DbSet<ApplicationUserClaim> AppApplicationUserClaims { get; set;
public DbSet<ApplicationRole> ApplicationRoles { get; set; }
public DbSet<ApplicationAction> ApplicationActions { get; set; }
public DbSet<ApplicationActionRole> ApplicationActionRoles { get; set; }
}
检查您的IdentityDbContext,ApplicationRoles已经定义了它可能是这样的:
public System.Data.Entity.DbSet<ApplicationRoles> Roles{ get; set; }
删除那个,当ApplicationRoles在XXXcontext中再次声明时,就会出现这个错误。。。。
您可以将Identity类指向数据库中自己的表,并根据需要扩展功能,而不是覆盖它们,如下所示
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
// Map Entities to their tables.
modelBuilder.Entity<MyUser>().ToTable("Users");
// Set AutoIncrement-Properties
modelBuilder.Entity<MyUser>().Property(r => r.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
// Override some column mappings that do not match our default
modelBuilder.Entity<MyUser>().Property(r => r.UserName).HasColumnName("username");
modelBuilder.Entity<MyUser>().Property(r => r.PasswordHash).HasColumnName("password");
modelBuilder.Entity<MyUser>().Property(r => r.Id).HasColumnName("lgn_key");
modelBuilder.Entity<MyUser>().Property(r => r.Email).HasColumnName("primaryEmail");
}