Here are the main strategies for loading related data: Lazy, Eager & Explicit loading.
Lazy Loading
Lazy loading is a feature in Entity Framework Core that allows related data to be automatically loaded from the database when accessed. With lazy loading, related entities are not loaded into memory until they are explicitly accessed, which can help improve performance and reduce memory usage.
Enabled by default in EF Core, but it can be disabled or configured on a per-query or per-entity basis.
This can result in additional database queries being executed, which can affect performance & costs.
To use lazy loading, you need to ensure that the related entities are marked as virtual properties in your entity classes.
var blog = context.Blogs.Find(10);
var posts = blog.Posts;
In the above code snippet, Line #1 returns a single Blog entry with an ID of 10, without any related data.
At Line #2, Entity Framework Core issues a query to load the Posts entity related to that particular blog.
When to use Lazy loading?
When the dataset is too huge.
When the entity relationships are complex.
When you are sure that you won't be using the loaded entities instantly.
This can lead to performance issues if used in loops or if the related entity is referenced multiple times in your code bases. For such scenarios, take a look at the next data loading strategy.
Eager Loading
Eager Loading is a feature in EF Core that allows you to retrieve related entities and the primary entity in a single database query. This is achieved using the Include() method.
var blogs = context.Blogs.Include(blog => blog.Posts).ToList();
In the above example, the blogs returned will also include the Posts property filled. You can also include multiple relationships in a single query. This may cause performance issues if not carefully designed. In some cases, it can even improve overall performance & costs by reducing the database calls!
When to use Eager Loading?
When you need to reduce the number of database calls.
When there are fewer child entities.
Only when you are absolutely sure that the loaded child entities will be used by you.
When to not use Eager Loading?
When related data is not needed for the current operation.
When the data set is too large and could lead to performance issues, and even might load bulk data into the memory.
Explicit loading
Technique for loading related entities on demand, similar to lazy loading, but with more control over when and how related entities are loaded. With explicit loading, you can load related entities using the DbContext.Entry method, which provides access to the underlying EntityEntry object representing the entity.
var blog = context.Blogs.Find(10);
context.Entry(blog).Collection(o => o.Posts).Load();
In the previous snippet, we're loading a blog entity from the database, and then using the Entry method to explicitly load the related Posts entities.
Explicit loading can be useful in scenarios where you need fine-grained control over how related entities are loaded, and can be used in conjunction with lazy loading and eager loading to provide a flexible and efficient data access strategy in EF Core.