EF Core包含多个级别的问题

时间:2017-08-24 20:51:49

标签: c# entity-framework asp.net-core entity-framework-core

使用 EF 6 ,我正在查询这样做,效果很好。

IQueryable<Student> query = _testHelper.buildQuerty(id, userId)
        .Include(x => x.Class)
        .Include(x => x.Subjects)
        .Include(x => x.Subjects.Select(y => y.Category));

问题: 这同样不适用于 EF Core 2.0

错误

  

发生System.ArgumentException HResult = 0x80070057 Message = The   属性表达'Subjects =&gt; {来自受试者中的受试者y选择   [y] .Category}'无效。表达式应代表属性   访问:'t =&gt; t.MyProperty”。有关包含的更多信息   相关数据,请参阅http://go.microsoft.com/fwlink/?LinkID=746393

打开链接,并像这样重构,但它仍然无法正常工作并给出相同的错误。

List<Student> query = _testHelper.buildQuerty(id, userId)
        .Include(x => x.Class)
        .Include(x => x.Subjects)
        .ThenInclude(Subjects => Subjects.Select(y => y.Category)).tolist();

问题出在哪里?

1 个答案:

答案 0 :(得分:6)

您错误地使用了ThenInclude的最后一个链。您应该像下面的代码一样使用它。

List<Student> query = _testHelper.buildQuerty(id, userId)
        .Include(x => x.Class)
        .Include(x => x.Subjects)
        .ThenInclude(subject => subject.Category);

使用此ThenInclude扩展方法,您将处理Subject的实例,因为最后一次使用Include处理该类型的集合。