我正在处理一个有两个上下文的应用程序,第一个是IdentityModels,第二个是GenericContext,我为两个上下文启用了迁移。
我可以生成IdentityModel的迁移,没有问题。
相反,当生成到GenericContext的迁移时,我有以下错误:
这可以通过onModelCreating中的以下几行来解决:
这不是一个有效的解决方案,因为生成GenericContext迁移会再次创建IdentityContext表:[AspNetUserLogins],[AspNetUserRoles]和[AspNetRolePermisions]
我该怎么办?
发布我的背景:
public class ApplicationUser : IdentityUser
{
[NotMapped]
public virtual Member Member { get; set; }
[NotMapped]
public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}
[Table("AspNetPermisions")]
public class Permision
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
public string Description { get; set; }
public bool passive { get; set; }
}
[Table("AspNetRolePermisions")]
public class RolePermision
{
public string RoleId { get; set; }
public int PermisionId { get; set; }
[ForeignKey("RoleId")]
public virtual ApplicationRole Role { get; set; }
[ForeignKey("PermisionId")]
public virtual Permision Permision { get; set; }
}
[Table("AspNetRoles")]
public class ApplicationRole : IdentityRole
{
public virtual string description { get; set; }
public bool passive { get; set; }
public virtual string type { get; set; }
public string createdBy { get; set; }
public DateTime createdDate { get; set; }
public string createdIn { get; set; }
public string modifiedBy { get; set; }
public DateTime? modifiedDate { get; set; }
public string modifiedIn { get; set; }
[ForeignKey("createdBy")]
public virtual ApplicationUser UserCreate { get; set; }
[ForeignKey("modifiedBy")]
public virtual ApplicationUser UserModify { get; set; }
public ICollection<ApplicationUserRole> UserRoles { get; set; }
public virtual ICollection<RolePermision> RolePermisions { get; set; }
}
[Table("AspNetUserRoles")]
public class ApplicationUserRole : IdentityUserRole
{
public override string UserId { get; set; }
public override string RoleId { get; set; }
[ForeignKey("UserId")]
public virtual ApplicationUser User { get; set; }
[ForeignKey("RoleId")]
public virtual ApplicationRole Role { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) { }
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.HasDefaultSchema("security");
modelBuilder.Entity<RolePermision>().HasKey(r => new { r.RoleId, r.PermisionId });
base.OnModelCreating(modelBuilder);
}
}
class GenericContext : DbContext
{
public GenericContext() : base("GenericConnection")
{
Configuration.LazyLoadingEnabled = false;
Configuration.ProxyCreationEnabled = false;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<IdentityUserLogin>().HasKey(l => new { l.UserId, l.LoginProvider, l.ProviderKey }).ToTable("AspNetUserLogins");
modelBuilder.Entity<ApplicationUserRole>().HasKey(r => new { r.RoleId, r.UserId }).ToTable("AspNetUserRoles");
modelBuilder.Entity<RolePermision>().HasKey(rp => new { rp.RoleId, rp.PermisionId }).ToTable("AspNetRolePermisions");
base.OnModelCreating(modelBuilder);
}
public DbSet<CatalogType> CatalogsTypes { get; set; }
public DbSet<Catalog> Catalogs { get; set; }
public DbSet<Module> Modules { get; set; }
public DbSet<Menu> Menus { get; set; }
public DbSet<Parameter> Parameters { get; set; }
public DbSet<Resource> Resources { get; set; }
public DbSet<Member> Members { get; set; }
public DbSet<RolMenu> RolMenus { get; set; }
public DbSet<ApplicationUser> ApplicationUsers { get; set; }
}