I was recently reading Rob Bagby’s blog about the ADO.NET Entity Framework and lazy loading.
In his post Rob discusses the concept of lazy loading. I want to explore that blog using some of the code we used.
Our code loops through both Blogs and Posts. So the question is, when does the queries for the related Posts take place? Post 1 and Post 2 are in the child table called Posts. So when do child tables get loaded?
What is the default behavior for code like this?
The next step is to run our application and see which queries come across the wire.
We will go to SQL Profiler next to see what happened.
You can see the lazy loading in process. If lazy loading works correctly, you should see 2 queries:
Lazy loading is postponing child or related tables until they are needed. In other words, developers want to avoid unnecessary queries until they are needed.
Lazy loading is a design pattern commonly used in computer programming to defer initialization of an object until the point at which it is needed. It can contribute to efficiency in the program’s operation if properly and appropriately used. The opposite of lazy loading is Eager Loading.
As a final note, see Rob’s blog to learn more.
Notice that the “context” object has a property called “DeferredLoadingEnabled,” which lets you control lazy loading functionality.
Thanks for reading ! BTW, this got posted from the a bus traveling on the Golden Gate Bridge.
The link to Rob Bagby’s blog is broken.