Entity FRM Core
Entity FRM Core
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.
codewithmukesh
var blog = context.Blogs.Find(10);
var posts = blog.Posts;
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.
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.
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.
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.
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.
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