我的堆栈是ASP.NET MVC 5,实体框架6.1,代码优先,SQL Server。
我正在处理一个涉及多所学校的申请,每所学校都有课程(每个都有部分)和学生。这些对象形成了相关对象的层次结构,每个对象都由一个学校实例创建。
迄今为止的基本布局:
一所学校有很多课程和学生
一门课程有很多部分
以下是模型的简化版本。
public class School
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Course> Courses { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
public class Student
{
public int Id { get; set; }
public int SchoolId { get; set; }
public virtual School School { get; set; }
public virtual ICollection<Enrolment> Enrolments { get; set; }
}
public class Course
{
public int Id { get; set; }
public string Title { get; set; }
public int SchoolId { get; set; }
public virtual School School { get; set; }
public virtual ICollection<CourseSection> CourseSections { get; set; }
}
public class CourseSection
{
public int Id { get; set; }
public int CourseId { get; set; }
public virtual Course Course { get; set; }
public virtual ICollection<Enrolment> Enrolments { get; set; }
}
还有其他模式和关系,但这应该足以构成我问题的基础。
课程部分与课程相关,而课程又与学校相关。给定一个课程部分,我可以确定它所属的学校,例如。
var school = givenSection.Course.School
。相反,给定一所学校,我可以获得属于该学校的课程部分。在代码中,只需要几次引用,而在数据库中,只需几次表连接。但它变得更加有趣。考虑下一个模型:
public class Enrolment
{
public int Id { get; set; }
public int StudentId { get; set; }
public int CourseSectionId { get; set; }
public virtual Student Student { get; set; }
public virtual CourseSection CourseSection { get; set; }
}
一
Enrolment
例如,学生和课程部分之间的多对多桥梁。获取一所学校的入学名单需要多个步骤,需要多个表连接。在一个可能会有大量记录的系统中,我担心这种设置的效率。再说一遍,应用程序被配置为延迟加载,所以也许没关系,我对EF的了解还不够。
为了简化数据检索,从以下两个方面中的一个或两个方面引用学校是否理想
CourseSection
和
注册
模型?如果层次结构中的所有模型都能够直接引用
School
他们属于谁?