我的上下文需要标识mvc#

我的上下文需要标识mvc#,c#,asp.net-mvc,entity-framework,asp.net-identity,C#,Asp.net Mvc,Entity Framework,Asp.net Identity,我正在处理一个有两个上下文的应用程序,第一个是IdentityModels,第二个是GenericContext,我已经为这两个上下文启用了迁移 我可以生成identitymodel的迁移,没有问题 相反,在生成到GenericContext的迁移时,我出现以下错误: 这可以通过onModelCreating中的以下行来解决: 这不是一个有效的解决方案,因为生成GenericContext迁移会再次创建IdentityContext表:[AspNetUserLogins]、[AspNetUs

我正在处理一个有两个上下文的应用程序,第一个是IdentityModels,第二个是GenericContext,我已经为这两个上下文启用了迁移

我可以生成identitymodel的迁移,没有问题

相反,在生成到GenericContext的迁移时,我出现以下错误:

这可以通过onModelCreating中的以下行来解决:

这不是一个有效的解决方案,因为生成GenericContext迁移会再次创建IdentityContext表:[AspNetUserLogins]、[AspNetUserRoles]和[aspNetRolePermissions]

我该怎么办

发布我的上下文:

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; }

}
公共类应用程序用户:IdentityUser
{
[未映射]
公共虚拟成员成员{get;set;}
[未映射]
公共虚拟ICollection用户角色{get;set;}
公共异步任务GenerateUserIdentityAsync(用户管理器)
{
//注意authenticationType必须与CookieAuthenticationOptions.authenticationType中定义的类型匹配
var userIdentity=wait manager.CreateIdentityAsync(这是DefaultAuthenticationTypes.ApplicationOkie);
//在此处添加自定义用户声明
返回用户身份;
}
}
[表(“aspnetpermissions”)]
公共类许可
{
[关键]
公共int Id{get;set;}
[必需]
公共字符串名称{get;set;}
公共字符串说明{get;set;}
公共布尔被动{get;set;}
}
[表(“aspnetrolepermissions”)]
公共类角色权限
{
公共字符串RoleId{get;set;}
public int permissionid{get;set;}
[外国钥匙(“RoleId”)]
公共虚拟应用程序角色{get;set;}
[外国密钥(“许可ID”)]
公共虚拟权限权限{get;set;}
}
[表(“AspNetRoles”)]
公共类应用程序角色:IdentityRole
{
公共虚拟字符串描述{get;set;}
公共布尔被动{get;set;}
公共虚拟字符串类型{get;set;}
通过{get;set;}创建的公共字符串
公共日期时间createdDate{get;set;}
公共字符串createdIn{get;set;}
公共字符串由{get;set;}修改
公共日期时间?修改日期{get;set;}
公共字符串modifiedIn{get;set;}
[外键(“createdBy”)]
公共虚拟应用程序用户UserCreate{get;set;}
[外键(“修改人”)]
公共虚拟应用程序用户用户修改{get;set;}
公共ICollection用户角色{get;set;}
公共虚拟ICollection角色权限{get;set;}
}
[表(“AspNetUserRoles”)]
公共类ApplicationUserRole:IdentityUserRole
{
公共重写字符串UserId{get;set;}
公共重写字符串RoleId{get;set;}
[外键(“用户ID”)]
公共虚拟应用程序用户{get;set;}
[外国钥匙(“RoleId”)]
公共虚拟应用程序角色{get;set;}
}
公共类ApplicationDbContext:IdentityDbContext
{
public ApplicationDbContext():base(“DefaultConnection”,throwifvv1schema:false){}
公共静态应用程序上下文创建()
{
返回新的ApplicationDbContext();
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
HasDefaultSchema(“安全性”);
modelBuilder.Entity().HasKey(r=>new{r.RoleId,r.permissionid});
基于模型创建(modelBuilder);
}
}
类GenericContext:DbContext
{
公共GenericContext():基(“GenericConnection”)
{
Configuration.LazyLoadingEnabled=false;
Configuration.ProxyCreationEnabled=false;
}
模型创建时受保护的覆盖无效(DbModelBuilder modelBuilder)
{
modelBuilder.Entity().HasKey(l=>new{l.UserId,l.LoginProvider,l.ProviderKey}).ToTable(“AspNetUserLogins”);
modelBuilder.Entity().HasKey(r=>new{r.RoleId,r.UserId}).ToTable(“AspNetUserRoles”);
modelBuilder.Entity().HasKey(rp=>new{rp.RoleId,rp.permissionid}).ToTable(“aspnetrolepermissions”);
基于模型创建(modelBuilder);
}
公共DbSet CatalogsTypes{get;set;}
公共数据库集目录{get;set;}
公共DbSet模块{get;set;}
公共数据库集菜单{get;set;}
公共数据库集参数{get;set;}
公共数据库集资源{get;set;}
公共数据库集成员{get;set;}
公共数据库集{get;set;}
公共数据库集应用程序用户{get;set;}
}

您需要两个不同的上下文有什么原因吗?我想与不同的数据管理器(Sql、Postgres)连接。AspNet表应该只属于一个上下文:IdentityModels。因此,ApplicationUsers表不在GenericContext中,该表应仅用于身份验证。如果需要,请创建一个可由标识引用的用户表。您需要两个不同上下文的原因是什么?我想与不同的数据管理器(Sql、Postgres)连接。AspNet表应该只属于一个上下文:IdentityModels。因此,ApplicationUsers表不在GenericContext中,该表应仅用于身份验证。如果需要,创建可由标识引用的用户表。