C# ASP.NET与ApplicationUser(Identity 2.0)的可选1:1关系

C# ASP.NET与ApplicationUser(Identity 2.0)的可选1:1关系,c#,asp.net,asp.net-mvc,entity-framework,ef-fluent-api,C#,Asp.net,Asp.net Mvc,Entity Framework,Ef Fluent Api,我有一个实习生,必须连接到应用程序用户。培训生模型具有与实际用户帐户无关的字段,但需要从应用程序用户获取诸如名字、姓氏之类的内容,因为用户不一定是培训生 但是,尽管在培训生中为用户定义了外键,我还是会遇到这些错误: 在模型生成过程中检测到一个或多个验证错误: MyApp.Models.IdentityUserLogin::EntityType “IdentityUserLogin”未定义密钥。为此定义密钥 EntityType。MyApp.Models.IdentityUserRole::Ent

我有一个
实习生
,必须连接到
应用程序用户
培训生
模型具有与实际用户帐户无关的字段,但需要从
应用程序用户
获取诸如
名字
姓氏
之类的内容,因为用户不一定是培训生

但是,尽管在
培训生
中为用户定义了外键,我还是会遇到这些错误:

在模型生成过程中检测到一个或多个验证错误:

MyApp.Models.IdentityUserLogin::EntityType “IdentityUserLogin”未定义密钥。为此定义密钥 EntityType。MyApp.Models.IdentityUserRole::EntityType “IdentityUserRole”未定义密钥。为此定义密钥 实体类型

…等等。总之,这是我当前的架构:

学员模式:

public class Trainee
{
    [Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
    public string TraineeId { get; set; }

    // other fields, not relevant
    [ForeignKey("TraineeId")]
    public virtual ApplicationUser User { get; set; }
}
应用程序用户和上下文:

    public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        // other arbitrary fields
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("SARSDbContext")
        {
        }
    }
公共类应用程序用户:IdentityUser
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
//其他任意字段
}

公共类应用程序dbContext:IdentityDbContext和for info,但我没有找到解决方案。

我刚刚意识到。。为什么有两个DBContext?可能是因为几年前我第一次学习mvc时,教程就是这么说的。直到现在才影响到我:/
public class SARSDbContext : DbContext
{
    public DbSet<Trainee> Trainees { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<Trainee>()
                    .HasRequired(c => c.User);
    }
}