C# 通过加入用户详细信息实体,从IdentityDbContext获取用户详细信息

C# 通过加入用户详细信息实体,从IdentityDbContext获取用户详细信息,c#,asp.net-mvc,entity-framework,asp.net-mvc-5,C#,Asp.net Mvc,Entity Framework,Asp.net Mvc 5,我有两个数据库上下文 它们是由以下引用的IdentityDbContext:ApplicationDbContext和DbEntites,即DbContext IdentityDbContext仅用于身份验证,UserManager不包含用户详细信息实体 DbEntites用于除用户还包含用户详细信息实体之外的其余实体 IdentityDbContext 我尝试在数据库中使用外键作为联系人ID。但还是一样的错误。我如何解决这个问题 请提供任何建议或解决方法 更新:基本上,我只想在Applicat

我有两个数据库上下文

它们是由以下引用的IdentityDbContext:ApplicationDbContext和DbEntites,即DbContext

IdentityDbContext仅用于身份验证,UserManager不包含用户详细信息实体

DbEntites用于除用户还包含用户详细信息实体之外的其余实体

IdentityDbContext

我尝试在数据库中使用外键作为联系人ID。但还是一样的错误。我如何解决这个问题

请提供任何建议或解决方法


更新:基本上,我只想在ApplicationDbContext中使用联系人实体,而不使用支持“ApplicationDbContext”上下文的模型自数据库创建以来已发生更改。错误,仍然可以在DbEntites中使用它。

我通过为AspNetUsers和AspNetRoles创建一个模型并在DbEntites中使用解决了这个问题。由于我的DbEntites已将Initializer设置为null,因此解决了我的问题

public class AspNetUsers
{
    [Key]
    public string Id { get; set; }
    public string UserName { get; set; }
    public int? ContactID { get; set; }

    [ForeignKey("ContactID")]
    public Contact Contact { get; set; }
    public ICollection<AspNetRoles> Roles { get; set; } 
}

public class AspNetRoles
{
    [Key]
    public string Id { get; set; }
    public string Name { get; set; }
    public ICollection<AspNetUsers> Users { get; set; } 
}
以十亿美元计

modelBuilder.Entity<AspNetUsers>()
                .HasMany(c => c.Roles)
                .WithMany(i => i.Users)
                .Map(t => t.MapLeftKey("UserId").MapRightKey("RoleId").ToTable("AspNetUserRoles"));


public DbSet<AspNetRoles> AspNetRoleses { get; set; }
public DbSet<AspNetUsers> AspNetUserses { get; set; }
这可能不是解决问题的理想方法,但究竟是哪一种

public class ApplicationUser : IdentityUser
{
   public int? ContactID { get; set; }

   [ForeignKey("ContactID")]
   public Contact Contact { get; set; }
}
public DbSet<Contact> Contacts { get; set; }
The model backing the 'ApplicationDbContext' context has changed since the database was created.
public class AspNetUsers
{
    [Key]
    public string Id { get; set; }
    public string UserName { get; set; }
    public int? ContactID { get; set; }

    [ForeignKey("ContactID")]
    public Contact Contact { get; set; }
    public ICollection<AspNetRoles> Roles { get; set; } 
}

public class AspNetRoles
{
    [Key]
    public string Id { get; set; }
    public string Name { get; set; }
    public ICollection<AspNetUsers> Users { get; set; } 
}
modelBuilder.Entity<AspNetUsers>()
                .HasMany(c => c.Roles)
                .WithMany(i => i.Users)
                .Map(t => t.MapLeftKey("UserId").MapRightKey("RoleId").ToTable("AspNetUserRoles"));


public DbSet<AspNetRoles> AspNetRoleses { get; set; }
public DbSet<AspNetUsers> AspNetUserses { get; set; }