Sobes.tech
Junior — Middle

Explain what is meant by loading related data in Entity Framework Core and how it is implemented.

sobes.tech AI

Answer from AI

In Entity Framework Core, loading related data (eager loading, lazy loading, explicit loading) means retrieving the main entity along with related data from other tables to avoid additional database queries.

  • Eager loading — related data is loaded immediately with the main entity using the Include method. For example:
var orders = context.Orders.Include(o => o.Customer).ToList();

Here, both orders and their related customers are loaded.

  • Lazy loading — related data is loaded automatically upon first access to the navigation property. This requires configuring proxies and using virtual properties.

  • Explicit loading — loading related data manually after retrieving the main entity, for example:

var order = context.Orders.First();
context.Entry(order).Reference(o => o.Customer).Load();

Thus, loading related data allows for efficient working with objects and their relationships, optimizing the number of database queries.