我试图让对象的Studies
属性Department
延迟加载,但它只会在我急切加载时加载。我已经在没有结果DepartmentId
的类中添加了 a ,Study
used和. 公共和私人二传手似乎没有区别(也不应该)。似乎没有任何效果。使用 EF 6.1。ICollection
ISet
virtual
public class Department
{
private Department() {}
public Department(DepartmentTitle title)
{
if (title == null) throw new ArgumentNullException();
this.Title = title;
this.Studies = new HashSet<Study>();
}
public int DepartmentId { get; private set; }
public virtual DepartmentTitle Title { get; private set; }
public virtual ICollection<Study> Studies { get; set; }
}
public class Study
{
private Study() {}
public Study(StudyTitle title, Department department)
{
if (title == null) throw new ArgumentNullException();
if (department == null) throw new ArgumentNullException();
this.Title = title;
this.Department = department;
}
public int StudyId { get; private set; }
public virtual StudyTitle Title { get; private set; }
public virtual Department Department { get; set; }
}
// Here I save the department and study objects
// I verified they exist in the database
var department = new Department(new DepartmentTitle("Department Title Here"));
department.Studies.Add(new Study(new StudyTitle("Study Title Here"), department));
data.SaveChanges();
// In a new database context
// Here I try to lazy load the Studies property, but get null
// It works if I add Include("Studies") before Where()
Department department = data.Departments.Where(d => d.Title.Value == "Department Title Here").Single();
System.Console.WriteLine(department.Studies.First().Title.Value);
// DepartmentTitle and StudyTitle are simple complex types with a Value property