Sub topic 11
Loading Related Data
In the last chapter, you linked a Blog to its Post rows with navigation properties. A navigation property is a property that points to a related entity, such as Blog.Posts or Post.Blog. EF Core does not fill these properties automatically when you run a query. You have to tell it when to load related data, and EF Core gives you three ways to do that.
Why Related Data Starts Empty
When you query blogs, EF Core sends SQL for the Blogs table only. Your blog.Posts list stays empty unless you ask for the posts. This is on purpose. Loading every related row by default could pull huge amounts of data you never use.
Eager Loading with Include
Eager loading means you load the related data in the same query. You use the Include method for this.
using Microsoft.EntityFrameworkCore;
using var db = new BloggingContext();
var blogs = await db.Blogs
.Include(b => b.Posts)
.ToListAsync();
foreach (var blog in blogs)
{
Console.WriteLine($"{blog.Url} has {blog.Posts.Count} posts");
}To go one level deeper, call ThenInclude after Include. For example, if each post had a Comments list, you would write .Include(b => b.Posts).ThenInclude(p => p.Comments).
Use eager loading when you already know you need the related data.
Explicit Loading with LoadAsync
Explicit loading means you load related data later, for an entity you have already loaded. You go through the context's Entry method.
using Microsoft.EntityFrameworkCore;
using var db = new BloggingContext();
var blog = await db.Blogs.FirstAsync();
// Load a collection navigation
await db.Entry(blog).Collection(b => b.Posts).LoadAsync();
// Load a single (reference) navigation
var post = await db.Posts.FirstAsync();
await db.Entry(post).Reference(p => p.Blog).LoadAsync();Use Collection for a list, like Posts. Use Reference for a single object, like Blog. Explicit loading works well when you only sometimes need the related data.
Lazy Loading
Lazy loading means EF Core loads related data the first time your code reads the navigation property. To turn it on, you need three things:
- Install the
Microsoft.EntityFrameworkCore.Proxiespackage. - Call
options.UseLazyLoadingProxies()inOnConfiguring. - Mark navigation properties as
virtual, for examplepublic virtual List<Post> Posts { get; set; } = new();.
Choosing a Strategy
| Strategy | How you ask | Good when |
|---|---|---|
| Eager | `Include`, `ThenInclude` | You always need the related data |
| Explicit | `Entry(...).Collection(...).LoadAsync()` | You need it only in some cases |
| Lazy | Proxies and `virtual` properties | Quick prototypes and small data sets |
Exercise
You load one blog with db.Blogs.FirstAsync(). Later, only if the user clicks "Show posts", you want its posts. Which loading strategy fits, and what line loads them?
Show answer
Explicit loading fits, because you only sometimes need the posts. Load them with await db.Entry(blog).Collection(b => b.Posts).LoadAsync();.
Exercise
Why does blog.Posts.Count print 0 after var blog = await db.Blogs.FirstAsync();, even though the blog has posts in the database?
Show answer
EF Core does not load related data unless you ask. The query only read the Blogs table. Add .Include(b => b.Posts) before FirstAsync() to load the posts in the same query.