Entity framework core EF Core两个表中的更多导航属性

Entity framework core EF Core两个表中的更多导航属性,entity-framework-core,Entity Framework Core,我正在尝试为两个表创建导航属性 这是代码 public class CourseMaster { public int Id { get; set; } public string Name { get; set; } public int? TeamLeaderId { get; set; } [ForeignKey("TeamLeaderId")] public StudentMaster TeamLeader { get; set; }

我正在尝试为两个表创建导航属性

这是代码

    public class CourseMaster
{
    public int Id { get; set; }
    public string Name { get; set; }

    public int? TeamLeaderId { get; set; }
    [ForeignKey("TeamLeaderId")]
    public StudentMaster TeamLeader { get; set; }

    public int? GroupLeaderId { get; set; }
    [ForeignKey("GroupLeaderId")]
    public StudentMaster GroupLeader { get; set; }

    public virtual ICollection<StudentMaster> Students { get; set; }
}

public class StudentMaster
{
    public int id { get; set; }
    public string Name { get; set; }

    public int FirstSemCourseId { get; set; }
    [ForeignKey("FirstSemCourseId")]
    public CourseMaster FirstSemCourse { get; set; }

    public int SecondSemCourseId { get; set; }
    [ForeignKey("SecondSemCourseId")]
    public CourseMaster SecondSemCourse { get; set; }

    public int ThirdSemCourseId { get; set; }
    [ForeignKey("ThirdSemCourseId")]
    public CourseMaster ThirdSemCourse { get; set; }


    public int CourseMasterId { get; set; }
    public CourseMaster Course { get; set; }

}

// Fluent API

        modelBuilder.Entity<StudentMaster>()
             .HasOne(p => p.Course)
             .WithMany(b => b.Students)
             .HasForeignKey(p => p.CourseMasterId);
公共类CourseMaster
{
公共int Id{get;set;}
公共字符串名称{get;set;}
public int?TeamLeaderId{get;set;}
[ForeignKey(“TeamLeaderId”)]
公共学生家长小组组长{get;set;}
public int?GroupLeaderId{get;set;}
[ForeignKey(“GroupLeaderId”)]
公共StudentMaster组组长{get;set;}
公共虚拟ICollection学生{get;set;}
}
公共班级学生校长
{
公共int id{get;set;}
公共字符串名称{get;set;}
public int FirstSemCourseId{get;set;}
[ForeignKey(“FirstSemCourseId”)]
公共课程主第一学期课程{get;set;}
public int SecondSemCourseId{get;set;}
[ForeignKey(“SecondSemCourseId”)]
公共课程管理员第二课程{get;set;}
public int ThirdSemCourseId{get;set;}
[ForeignKey(“ThirdSemCourseId”)]
公共课程主管第三个课程{get;set;}
公共int CourseMasterId{get;set;}
公共课程主课程{get;set;}
}
//Fluent API
modelBuilder.Entity()
.HasOne(p=>p.Course)
.有很多(b=>b学生)
.HasForeignKey(p=>p.CourseMasterId);
但是,当我创建迁移时,会出现以下错误

无法确定“StudentMaster”类型的导航属性“CourseMaster.TeamLeader”表示的关系。手动配置关系,或者从模型中忽略此属性

我遵循的过程是正确的还是应该创建中间类

或者我应该如何创建类

谢谢你的帮助


谢谢

您需要为每个实体定义键,如果您使用数据注释,则不需要使用fluent API。这是我的错,实际上这两个类都继承了实体类,如下所示。公共接口IEntity{int Id{get;set;}字节[]时间戳{get;set;}}公共抽象类实体:IEntity{[Key]公共int Id{get;set;}[Timestamp]公共字节[]时间戳{get;set;}然后我也得到了相同的错误。我尝试添加[Key][DatabaseGenerated(DatabaseGeneratedOption.Identity)]到Id字段。然后我也收到了相同的错误。感谢您的帮助。