我正在尝试使用Entity Framework 6(代码优先)改进代码中的某些内容。我有以下模型类(仅包括重要的类):
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
public List<CourseRegistration> Courses { get; set; }
}
public class Course
{
public int ID { get; set; }
public string Name { get; set; }
public Department Department { get; set; }
public CourseType CourseType { get; set; }
public Teacher Teacher { get; set; }
public CourseDescription CourseDescription { get; set; }
}
public class CourseRegistration
{
public int Id { get; set; }
public Student Student { get; set; }
public Course Course { get; set; }
public string Session { get; set; }
public List<Result> Results { get; set; }
}
在我的项目中,我需要始终获取Course类的所有子实体。要做到这一点,我可以使用工作得很好的Include()方法:
IQueryable<Course> completeCourse = db.Courses
.Include(r => r.CourseType)
.Include(r => r.CourseDescription)
.Include(r => r.Department)
.Include(r => r.Teacher);
现在问题就在于此。当我从CourseRegistration查询课程时,我仍然需要包含所有子实体,但语法有点不同:
IQueryable<CourseRegistration> registrations = db.CourseRegistrations
.Include(r => r.Course)
.Include(r => r.Course.CourseDescription)
.Include(r => r.Course.CourseType)
.Include(r => r.Course.Department)
.Include(r => r.Course.Teacher)
.Include(r => r.Student)
.Include(r => r.Results);
这不是真的干。我正在寻找一种方法来确保无论查询的来源是什么,课程类总是会被完全加载。有人知道我可以将我的课程类所需的包含“链接”到任何查询吗?
有什么想法吗?
由于