0

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.

10
  • In your last sentence, did you mean to say "In Eager load it always...."? Commented Dec 24, 2015 at 10:02
  • No @DavidG, its Lazy load. Actually i am facing this issue. Lazy loading load all Categories and products which shouldn't be Commented Dec 24, 2015 at 10:04
  • Lazy load will always load anything though, even viewing in the debugger will cause things to load. Commented Dec 24, 2015 at 10:04
  • Use SQL Profiler to see when the query to load the related data executed Commented Dec 24, 2015 at 10:05
  • 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? Commented Dec 24, 2015 at 10:06

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

9 Comments

Okay, it means when I'll see resuts using debugger it request to load related entities in Lazy load. Right ?
@Gerry Yes: the debugger tries to access the navigation properties to display them, so it executes a new SQL request to lazy load those properties
Can we trace SQL requests in this case using VS or SQL Management studio ?
@Gerry Yes. Personally I use SQL Server Profiler to do the job. It's a built-in tool provided with SQL Server and Management Studio.
@AlexanderDerck Nowadays, the Express version also includes the profiler. I think it was SQL Server 2012 SP1 that first provided it, but 2014 definitely has it.
|
2

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.

2 Comments

Okay @Slava. it there any way to see the difference. i means the difference in back end quires generated for this linq code?
First, try to look at variable query without ToList() postfix: var query = objDb.Categories in case of LazyLoading (you won't see anything about Products). Also if you use MS SQL you can try to use ms sql profiler.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.