Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example of N+1 problem
- var orders = context.Orders.ToList();
- foreach(var order in orders)
- {
- var customer = context.Customers.Find(order.CustomerId);
- Console.WriteLine($"Order {order.Id} belongs to {customer.Name}");
- }
- // Avoiding N+1 problem by eager loading
- var orders = context.Orders.Include(o => o.Customer).ToList();
- foreach(var order in orders)
- {
- var customer = order.Customer;
- Console.WriteLine($"Order {order.Id} belongs to {customer.Name}");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement