在Include Repository Pattern Returning null中选择



我有一个数据库模型,看起来像这样:

  1. 投诉(T1)
  2. 监控投诉(T2)
  3. 监测说明(T3)

我的每个班级如下:

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关系的问题上使用了这个解决方案,比如这个:实体框架-包括多个级别的属性。

使用then-include解决了我的问题。

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;
    }
}

最新更新