score:2

Accepted answer

When you are writing this:

context.LettingProjects.Where(query).Select(s => s.Call).ToList();

EF will simply query for a single table.

However, as you probably configured the relationships in your models to contain other Tables too (ICollection<EntityA>), they will contain a null value because EF will not send a query asking for their data.

My question is, how does this even work? I don't know how EF handles this in the background, and it seems like a gotcha waiting to happen.

The answer to your question now should be simple:
when you write .Include(s => s.OtherTable) you are asking EF to join your current table data with the one you specified.

EF will use the Foreign Key relationships and will give you back a collection of items with the data for the related tables, too (not null version). That's why it will not fail.


This is very similar to using the LINQ to Entities Join method, but in this case, it's requiring you to write much less code unless you want to control the join operation by yourself.


Related Articles