我与Entity Framework建立了多对多关系,如下所示:
public class Student
{
public int StudentId { get; set; }
public string Name { get; set; }
public ICollection<StudentCourse> StudentCourses { get; set; }
}
public class Course
{
public int CourseId { get; set; }
public string Name { get; set; }
public ICollection<StudentCourse> StudentCourses { get; set; }
}
public class StudentCourse
{
public int StudentId { get; set; }
public Student Student { get; set; }
public int CourseId { get; set; }
public Course Course { get; set; }
}
这很有效,我有学生和我有课程,学生课程是他们之间的多对多关系。
我还有一个顾问课,其中有一系列由顾问负责的学生课程:
public class Advisor
{
public int AdvisorId { get; set; }
public ICollection<StudentCourse> StudentCourses { get; set; }
}
我想获得他们负责的顾问和学生课程的集合,以及学生和课程对象(如姓名)的数据属性,这些都是一次性的。这对我有用:
var advisors = await _dbContext.Advisors
.Include(a => a.StudentCourses)
.ThenInclude(sc => sc.Student)
Include(a => a.StudentCourses)
.ThenInclude(sc => sc.Course)
.ToListAsync();
但这是我能做到的唯一方法吗?将重复的Include语句
看似错误