Add example Eager Loading function.

This commit is contained in:
Kevin Matsubara 2025-04-07 13:50:57 +02:00
parent 64965c3fef
commit 8103c682cf

View File

@ -15,6 +15,23 @@ context.Database.EnsureCreated();
// Rather than raise: "System.InvalidOperationException: Sequence contains no elements." // Rather than raise: "System.InvalidOperationException: Sequence contains no elements."
// var firstCoach = await context.Coaches.FirstOrDefaultAsync(); // var firstCoach = await context.Coaches.FirstOrDefaultAsync();
async Task EagerLoadingExample()
{
var leagues = await context.Leagues
.Include(l => l.Teams)
.ThenInclude(t => t.Coach)
.ToListAsync();
foreach (var league in leagues)
{
Console.WriteLine($"League - {league.Name}");
foreach (var team in league.Teams)
{
Console.WriteLine($"* {team.Name} - {team.Coach.Name}");
}
}
}
async Task InsertRelatedData() async Task InsertRelatedData()
{ {
// Insert record with a Foreign Key. // Insert record with a Foreign Key.