我正在使用实体框架5与Code First和延迟加载禁用。鉴于以下几点:为什么我需要在我的子类中使用EF5的虚拟属性?
public class Department
{
public int DepartmentID { get; set; }
public string Name { get; set; }
public virtual ICollection<Course> Courses { get; set; }
}
public class Course
{
public int CourseID { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
public int DepartmentID { get; set; }
public virtual Department Department { get; set; }
}
// Mapping Relationships for Course
<code here>
this.HasRequired(t => t.Department)
.WithMany(t => t.Courses)
.HasForeignKey(d => d.DepartmentID);
有人可以解释为什么我会需要有:如果我完全删除这一行
public virtual Department Department { get; set; }
会发生什么?我不能做什么?据我所见,我唯一不能做的就是使用Entity Framework来检查我的外键。不过,我已经在数据库中设置了这个设置,所以我仍然会从Db中得到一个错误。
但是我不知道,因为我有DepartmentID? – Alan2
DepartmentId很有用,但Department属性包含Department实体的所有列。如果您需要了解有关部门的详细信息(名称等),那么这很方便 - 在使用课程时 – TGH