Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Rajanikant HawaldarRank #110 · 20.2K reputationPosted Oct 19, 2022, 1:15 AM EDT
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.
UserDeatils ud = usr.UserDetails; // UserDetails are loaded hereBrahma Prakash ShuklaRank #633 · 3.8K reputationPosted Oct 19, 2022, 5:26 AM EDT
Please see this link- https://www.imperva.com/learn/performance/lazy-loading/#:~:text=While%20lazy%20loading%20delays%20the,entities%20referenced%20by%20a%20resource.