0
votes

i have tried to implement concepts of lazy and Eager loading in .Net Entity Framework. I have two tables Products & Categories, a category can have multiple products. Please check attached screenshot.

  1. Default behavior [Lazy Loading true]

It shows all Categories and its related products as well. But as per definition it should get only categories at this time.

  1. Eager Loading [Lazy Loading false] without keyword include In Eager loading it should load all Categories and Products in one query. But here it brought only categories but no products. This is totally different from its definition. (may be due to missing include keyword)

  2. Eager Loading [Lazy Loading false] with keyword include Again this loaded all categories and its products, this is correct behavior of Eager loading but what is the different in results in this case and in Lazy load (Picture 1)

In Lazy load it always load Categories and related products (whether we use include or not) . which is not correct. Please clarify me for this confusion.

2
In your last sentence, did you mean to say "In Eager load it always...."? - DavidG
No @DavidG, its Lazy load. Actually i am facing this issue. Lazy loading load all Categories and products which shouldn't be - Gurmeet
Lazy load will always load anything though, even viewing in the debugger will cause things to load. - DavidG
Use SQL Profiler to see when the query to load the related data executed - Mark PM
Okay @DavidG, But how can i see the actual difference if i want to see. Can we see difference in SQL query generated for this LINQ code? - Gurmeet

2 Answers

3
votes

In Lazy load it always load Categories and related products (whether we use include or not) . which is not correct

Actually you don't load them in your code, it's the debugger that requests them to load, as you are currently viewing the list of the properties of your entities.

2
votes

I think all works as expected, but you don't see it. In case of LazyLoading initial loaded only Categories, but when you debug your code and want to see Products - they loaded only at this moment and very quickly and you not observe difference.