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