An Enthusiastic Programmer

EF Core Eager Loading

|

I supposed that you had known Eager Loading means that related entities are loaded as part of the initial query.

This project can be found in Github

use Include method or ThenInclude method to complete an eager loading. the Include method can specify what navigation property you want to include. the ThenInclude can allow you to continue including further levels of related entities.

public async Task<List<Team>> loadTeamsAsync()
{
    using (var context = new SchoolContext())
    {
        return await context.teams.
            Include(t => t.players).
                ThenInclude(p => p.addresses).
            ToListAsync();
    }
}

Caution:

Since version 3.0.0(.net core 3.0.0), each Include will cause an additional JOIN to be added to SQL queries produced by relational providers, whereas previous versions generated additional SQL queries. This can significantly change the performance of your queries, for better or worse. In particular, LINQ queries with an exceedingly high number of Include operators may need to be broken down into multiple separate LINQ queries in order to avoid the cartesian explosion problem.

Comments