Junior — Middle
Mis roll on Include meetodil Entity Frameworkis ja kuidas seda kasutatakse seotud andmete laadimiseks?
sobes.tech AI
Vastus AI-lt
The Include method in Entity Framework is used for eager loading of related data. By default, EF loads only the main object, and related entities (such as related tables) are loaded lazily or not at all.
Include allows explicitly specifying which related data should be loaded along with the main object to avoid additional database queries.
Example of usage:
var orders = context.Orders
.Include(o => o.Customer) // loads related customer data
.Include(o => o.OrderItems) // loads order items
.ToList();
This way, when the query is executed, orders are loaded together with customer information and order items in a single query, improving performance and reducing the number of database calls.