Sobes.tech
Junior — Middle

How can related entities be loaded in Entity Framework?

sobes.tech AI

Answer from AI

In Entity Framework, the following approaches are used to load related entities:

  1. Eager Loading – related data is loaded together with the main entity using the Include method:
var orders = context.Orders.Include(o => o.OrderItems).ToList();
  1. Lazy Loading – related entities are loaded automatically upon first access if lazy loading support is enabled.

  2. Explicit Loading – related entities are loaded manually after the main entity has been loaded:

var order = context.Orders.Find(id);
context.Entry(order).Collection(o => o.OrderItems).Load();

The choice of method depends on performance requirements and data structure.