Eager Loading
1. The Problem
You've got an ASP.NET Core application that displays orders for a customer.
An order has its own information, but you also need related data such as:
- Customer
- Order Items
- Product details
For example, your API needs to return something like:
Order #10245 Customer: Rahul Sharma Items: - Laptop - Wireless Mouse - Keyboard
The data is related in the database:
Customer
|
+---- Orders
|
+---- Order Items
|
+---- Products
A .NET engineer might load the order first and then, while processing the response, access each related object.
That can create a serious production problem.
Imagine you load 100 orders.
Then for every order, you access its customer and order items.
You may end up making many additional database queries.
The application works.
The data is correct.
But the database is suddenly doing far more work than you expected.
2. What's Actually Going Wrong?
The problem is often caused by lazy loading.
The application loads the main entity:
Orders
Then, when related data is accessed:
Order.Customer Order.Items
additional database queries may be triggered to retrieve that related data.
For one order, this may not look like a problem.
But consider 100 orders:
Get 100 Orders
|
+---- Get Customer
+---- Get Items
+---- Get Customer
+---- Get Items
+---- Get Customer
+---- Get Items
...
Now you have potentially dozens or hundreds of additional database operations.
This is commonly known as the N+1 query problem.
The application makes:
1 query to get the orders + N additional queries for related data
For a small dataset, you might never notice.
In production, with large datasets and concurrent users, it can become expensive.
3. The Concept
Eager Loading means:
Load the related data you know you'll need as part of the initial database query.
Instead of saying:
“Give me the orders. I'll fetch related data whenever I need it.”
you say:
“Give me the orders together with the related data I need for this operation.”
For example:
Orders
|
+---- Customer
|
+---- Order Items
|
+---- Product
The important point is not necessarily that the database must execute exactly one SQL statement.
The important point is:
The application explicitly asks for the related data it needs up front instead of triggering additional queries later while processing the objects.
4. How Do You Fix It?
Let's say your API needs to display an order together with:
- Customer
- Order Items
- Product information
With eager loading, the application tells EF Core about those relationships as part of the initial data retrieval.
Conceptually:
Request | v ASP.NET Core API | v "Load Orders + Customer + Order Items + Products" | v Database | v Complete data graph | v ASP.NET Core API | v Response
Instead of this:
Get Orders
|
v
Process Order
|
+---- Query Customer
|
+---- Query Items
|
+---- Query Product
you establish the required relationships before retrieving the data.
In EF Core
EF Core supports eager loading through its relationship-loading capabilities.
The query can explicitly request the related entities that the API needs.
The exact implementation depends on the model and query, but the important architectural decision is:
Load the required related data deliberately, rather than allowing access to related properties to trigger unexpected database calls.
5. When Should You Use Eager Loading?
Eager loading makes sense when you already know that the related data is required.
For example, an Order Details page needs:
Order Customer Order Items Product
There is little value in retrieving only the Order and then going back to the database repeatedly for the rest.
Eager loading can make the data access pattern explicit and predictable.
It is particularly useful when:
- The related data is definitely required.
- You are building a response that needs multiple related entities.
- You want to avoid unexpected additional database queries.
- You want the database access pattern to be clear from the query.
But there is an important warning.
Don't eagerly load everything.
6. What Happens If You Load Too Much?
Imagine an application with:
Customer
|
+---- Orders
|
+---- Items
|
+---- Products
|
+---- Reviews
+---- Categories
+---- Suppliers
You might think:
“Why not just load everything?”
Because now your query can become huge.
You may retrieve a lot of data that the API doesn't actually need.
The result can be:
Too little data
|
v
Many database queries
|
v
Performance problem
Too much data
|
v
Huge query / large result
|
v
Performance problem
So eager loading is not about loading more data.
It is about loading the right related data at the right time.
A good engineer asks:
“What data does this operation actually need?”
Then loads that data deliberately.
7. Visualize It in Everyday Terms
Imagine you're going on a road trip.
You know before leaving that you'll need:
- Your driver's license
- Car documents
- Sunglasses
- Phone charger
- Water
You have two choices.
Option 1: Keep going back home
You leave home.
Then realize:
“I need my charger.”
You go back.
Then:
“I also need my sunglasses.”
You go back again.
Then:
“Where are the car documents?”
Another trip back.
That's like repeatedly querying the database for related data.
Option 2: Pack everything you know you'll need
Before leaving, you collect everything required.
Then you leave.
That's Eager Loading.
In this analogy:
- Your house = Database
- You = Application
- Things you pack = Related data
- Packing them before leaving = Eager Loading
You already know what you'll need, so you get it up front.
The opposite would be:
“I'll leave now and come back whenever I need something.”
That's like repeatedly going back to the database for related data.
But you wouldn't pack your entire house into the car.
That's the other lesson:
Eager Loading means loading the related data you know you'll need, not everything that exists.
8. What Should You Remember?
Think about Eager Loading as:
REQUEST
|
v
What related data
will I need?
|
v
Load it up front
|
v
DATABASE
|
v
Complete data needed
|
v
RESPONSE
The key points:
- Eager Loading retrieves related data up front.
- It is useful when you already know the related data is required.
- It can help avoid N+1 query problems caused by loading relationships one at a time.
- It makes the required data relationships explicit.
- Don't eagerly load relationships you don't actually need.
The mental model is simple:
If you know you'll need the related data, ask for it up front.
9. .NET + Azure Context
From a .NET engineer's perspective, the important question isn't:
“Should I always use eager loading?”
It is:
“For this operation, do I already know which related data I need?”
If the answer is yes, eager loading can make that requirement explicit and help avoid a chain of unexpected database queries.
And remember:
Eager Loading is not “load everything.”
It is: