我想热切地加载具有特定Id的A类实例。然后我想。包括与A实例相关的每个B类实例和.Include与B实例相关的每个C实例。
这不起作用:
var schoolyear = _context.Schoolyears
.Include(s => s.SchoolclassCodes)
.Include(s => s.TimeTableEntries)
.SingleOrDefault(s => s.Id == schoolyearId);
最后一个.Include是错误的。它必须与.SchoolclassCodes。
相关对于stronly类型的.Include()是不可能的,或者我是否必须使用weaked类型样式:
var schoolyear = _context.Schoolyears
.Include("SchoolclassCode.TimeTableEntries")
.SingleOrDefault(s => s.Id == schoolyearId);
class Schoolyear
{
public int Id { get; set;}
public ICollection<SchoolclassCode> {get;set;}
}
class SchoolclassCode
{
public ICollection<TimeTableEntry> {get;set;}
}
class TimeTableEntry
{
}
答案 0 :(得分:1)
您也可以使用强类型方法。
只需在Include
中创建一个选择查询,直到到达应加载的层次结构中的最后一个实体:
var schoolyear = _context.Schoolyears
.Include(s => s.SchoolclassCodes
.Select(t => t.TimeTableEntries));