EF Core Including multiple levels issue

asp.net-core c# entity-framework entity-framework-core

Question

With EF 6, I was querying like this and it was working nice.

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

Problem: The same does not work in EF Core 2.0.

Error

System.ArgumentException occurred HResult=0x80070057 Message=The property expression 'Subjects => {from Subjects y in Subjects select [y].Category}' is not valid. The expression should represent a property access: 't => t.MyProperty'. For more information on including related data, see http://go.microsoft.com/fwlink/?LinkID=746393.

Opened up link, and refactored like this but it still not working and gives same error.

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

Where is the problem?

1
1
8/24/2017 9:55:20 PM

Accepted Answer

You're using the last chain of ThenInclude wrongly. You should use it like the code below.

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

With this ThenInclude extension method, you're dealing with an instance of Subject because of the last of use of Include which deal with a collection of that type.

6
8/24/2017 9:18:43 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow