↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Using lazy loading in Entity Framework Core 8

The article shows how to opt into lazy loading in EF Core 8 so related data is not loaded until it is needed. It uses a Customer example with navigation properties and explains that, without eager loading or lazy loading, those properties stay null. It also notes that the debugger can make lazy-loaded data appear loaded before the application code accesses it.

Reading notes
#

  • EF Core 8 does not populate related navigation properties automatically unless eager loading or lazy loading is enabled.
  • A Customer entity is used as the example, with CustomerRank and AddressCustomers as related properties.
  • The ILazyLoader service from Microsoft.EntityFrameworkCore.Infrastructure can be injected into the entity through a private constructor.
  • A navigation property can call LazyLoader.Load in its getter so the related value is loaded on access.
  • If injecting behavior into entities feels unclean, the article shows using Fluent API in DbContext configuration instead.
  • AutoInclude is used on Customer.AddressCustomers and Customer.CustomerRank in the shown modelBuilder configuration.
  • The article also shows manual loading with DbContext.Entry, then Reference or Collection, followed by Load.
  • Lazy loading still happens only when the navigation property is accessed.
  • In Visual Studio debugging, data may look loaded because the debugger can read lazy-loaded fields too.
  • In the example, the private field _customerRank is initially null and is loaded when CustomerRank is accessed.