Eager loading for one to many to one in EF Core

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

Question

I have a database schema just like below:

ER Model

Here is the code that I need to do eager loading for my data:

var item = db.Course
    .Include(p => p.CourseInstructor)
    .FirstOrDefault(p => p.CourseId == courseId);

if (item == null)
{
    return NotFound();
}

return (from p in item.CourseInstructor
        select p.Instructor).ToList();

All I can get are the null on the p.Instructor navigation property.

I was tried to use ThenInclude, but I don't know how to code it right.

Wrong trial

How can I made this code works as expected to get the full Person data using Eager loading technique?

1
0
12/13/2018 5:50:40 PM

Accepted Answer

This is a known Intellisense issue with the ThenInclude overload for collection type navigation properties, tracked by the Completion missing members of lambda parameter in fault tolerance case #8237 Roslyn GitHub issue.

Until it gets fixed, simply type the name of the property and it will compile successfully and work as expected.

.ThenInclude(mu => mu.ParseSubTrees) 

Update: Now it's even specifically mentioned in the Including multiple levels section of the EF Core documentation:

Note

Current versions of Visual Studio offer incorrect code completion options and can cause correct expressions to be flagged with syntax errors when using the ThenInclude method after a collection navigation property. This is a symptom of an IntelliSense bug tracked at https://github.com/dotnet/roslyn/issues/8237. It is safe to ignore these spurious syntax errors as long as the code is correct and can be compiled successfully.

41
10/10/2018 8:06:29 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