我有一个如下所示的数据库模型:
我的每个类看起来如下:
class Complaints{
//props
public virtual ICollection<MONITORING> MONITORINGs { get; set; }
}
class Monitoring{
//props
public virtual ICollection<MonitoringNotes> MONITORINGNotes { get; set; }
}
class MonitoringNotes{
//props
}
当我运行存储库以获取所有数据时,我获取了所有数据
public IEnumerable<Complaints> GetAll()
{
try
{
return _context.Complaints
.Include(t => t.MONITORINGs)
.OrderBy(t => t.FileNum)
.ToList();
}
catch (Exception ex)
{
_logger.LogError("Could not get complaint with checklist", ex);
return null;
}
}
但是当我使用select将注释添加到监视器时,它返回Null:
public IEnumerable<Complaints> GetAll()
{
try
{
return _context.Complaints
.Include(t => t.MONITORINGs.Select(t3=>t3.MONITORINGNotes )
.OrderBy(t => t.FileNum)
.ToList();
}
catch (Exception ex)
{
_logger.LogError("Could not get complaint with checklist", ex);
return null;
}
}
当我输入select时,我会得到注释的Intellisense,所以我不认为它是我的实体。如果我正确使用select语句,有人能指出我正确的方向吗?我在许多关于包含3级关系的问题上使用了这个解决方案,例如:Entity Framework - Include Multiple Levels of Properties。
答案 0 :(得分:1)
使用然后包括解决了我的问题。
public IEnumerable<Complaints> GetAll()
{
try
{
return _context.Complaints
.Include(t => t.MONITORINGs)
.ThenInclude(t3=>t3.MONITORINGNotes )
.OrderBy(t => t.FileNum)
.ToList();
}
catch (Exception ex)
{
_logger.LogError("Could not get complaint with checklist", ex);
return null;
}
}