0% found this document useful (0 votes)
12 views10 pages

Entity FRM Core

Uploaded by

sonalisemalty27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

Entity FRM Core

Uploaded by

sonalisemalty27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

STRATEGIES FOR LOADING RELATED DATA IN

ENTITY
FRAMEWORK CORE
EAGER LOADING LAZY LOADING EXPLICIT LOADING

codewithmukesh
LOADING RELATED DATA IN ENTITY FRAMEWORK CORE

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.

codewithmukesh
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.

codewithmukesh
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.

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.

codewithmukesh
LOADING RELATED DATA IN ENTITY FRAMEWORK CORE

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!

codewithmukesh
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.

codewithmukesh
LOADING RELATED DATA IN ENTITY FRAMEWORK CORE

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();

codewithmukesh
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.

codewithmukesh
Join the Practical .NET 8 Series!
Subscribe to my .NET Newsletter to join the Roadmap for free, and
receive 1 AWESOME mail every week related to practical .NET 8
Guide to build Clean Architecture Solutions.

Link in Description
WAS THIS
HELPFUL?
Share with a friend who needs it!

mukesh murugan
@iammukeshm

Follow me for more!

You might also like