What is eager and lazy loading?
Loading
What is eager and lazy loading?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Rajanikant HawaldarPosted Oct 19, 2022, 5:15 AM
Eager Loading:
Eager Loading is the opposite of Lazy Loading. It loads the related data in scalar and navigation properties along with query results at the beginning.
Eager Loading helps you to load all your needed entities at once; i.e., all your child entities will be loaded at single database call. This can be achieved, using the Include method, which returs the related entities as a part of the query and a large amount of data is loaded at once.
For example, you have a User table and a UserDetails table (related entity to User table), then you will write the code given below. Here, we are loading the user with the Id equal to userId along with the user details.
If you have multiple level of child entities, then you can load, using the query given below.
Lazy Loading:
It is the default behavior of an Entity Framework, where a child entity is loaded only when it is accessed for the first time. It simply delays the loading of the related data, until you ask for it.
Lazy Loading brings in the related data on an as-needed basis, meaning that when something touches the Contact property of a Friend, EF loads the data by sending an additional query to the database.
For example, when we run the query given below, UserDetails table will not be loaded along with the User table.
It will only be loaded when you explicitly call for it, as shown below.
Brahma Prakash ShuklaPosted Oct 19, 2022, 9:26 AM
Please see this link- https://www.imperva.com/learn/performance/lazy-loading/#:~:text=While%20lazy%20loading%20delays%20the,entities%20referenced%20by%20a%20resource.