From 3652a69730f8bfd34c162f15610b23445f09b484 Mon Sep 17 00:00:00 2001 From: Kevin Matsubara Date: Mon, 7 Apr 2025 14:02:25 +0200 Subject: [PATCH] Add example Explicit Loading function. --- EntityFrameworkCore.Console/Program.cs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/EntityFrameworkCore.Console/Program.cs b/EntityFrameworkCore.Console/Program.cs index 9d79533..360d167 100644 --- a/EntityFrameworkCore.Console/Program.cs +++ b/EntityFrameworkCore.Console/Program.cs @@ -15,6 +15,29 @@ context.Database.EnsureCreated(); // Rather than raise: "System.InvalidOperationException: Sequence contains no elements." // var firstCoach = await context.Coaches.FirstOrDefaultAsync(); +await ExplicitLoadingExample(); + +async Task ExplicitLoadingExample() +{ + var league = await context.FindAsync(1); + if (!league.Teams.Any()) + { + Console.WriteLine("Teams have not been loaded."); + } + + await context.Entry(league) + .Collection(l => l.Teams) + .LoadAsync(); + + if (league.Teams.Any()) + { + foreach (var team in league.Teams) + { + Console.WriteLine($"{team.Name}"); + } + } +} + async Task EagerLoadingExample() { var leagues = await context.Leagues