ADO.NET Entity Framework and .NET 4 & SQL Profiler : Lazy Loading of Entities
I was recently reading Rob Bagby’s blog about the ADO.NET Entity Framework and lazy loading.
https://www.robbagby.com/entity-framework/is-lazy-loading-in-ef-4-evil-or-the-second-coming/ |
What this blog post will address:
- The meaning of Lazy Loading
- Scenarios where it makes sense
- Using SQL Profiler to validate the SQL statements 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.
Source code to Previous Blog Posts
Previous Blog Entries
- https://blogs.msdn.com/brunoterkaly/archive/2010/01/30/ado-net-entity-framework-and-net-4-visual-studio-2010-modeling-tools-writing-code-and-working-with-entities-to-add-data.aspx
- https://blogs.msdn.com/brunoterkaly/archive/2010/01/25/ado-net-entity-framework-and-net-4-how-to-use-visual-studio-2010-modeling-tools-to-build-a-database.aspx
- https://blogs.msdn.com/brunoterkaly/archive/2010/02/01/ado-net-entity-framework-4-0-introduction-to-quadrant.aspx
Entities Encapsulate 1-to-many relationships
But when is the data loaded – And what does “Lazy” Mean?
- 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.
Can you spot the issue of lazy loading in the Blog entity?
- Notice the Blog entity has an array of Posts
- The main question is, “When Will Posts get loaded from the database?”
- Will it get loaded when the Blog object loads?
- Or will loading get deferred until execution of the code when Posts are needed?
Purpose of Screen: You can see the a Blog object has many Post objects |
We will use SQL Profiler to answer these questions
SQL Profiler allows us to see which queries are hitting the database
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?
Purpose of Screen: To illustrate the concept of lazy loading. When does “Post 1,” “Post 2” etc get loaded? |
Purpose of Screen: To run SQL Server Profiler |
Starting a Profiler Trace
Start at the “File” menu
- Choose “New Trace”
Purpose of Screen: To start a New Trace |
Default Trace Properties
The “Standard” Template will work
Purpose of Screen: Choosing the trace template, which defines what the trace will include. The “Standard (default) will work for us. |
Trace Started
We are ready to start viewing results
The next step is to run our application and see which queries come across the wire.
Purpose of Screen: Default startup screen |
Run our ADO.NET Entity Framework Application (WPF – based)
Go to the “Debug”menu and choose “Start Debugging”
- Our app is started and after we hit “View Data” the ADO.NET Entity Framework code will execute
Purpose of Screen: Starting our application |
Click “View Data”
This will execute the ADO.NET Entity Framework code
We will go to SQL Profiler next to see what happened.
Purpose of Screen: Our main application. Source code is available. |
Lazy Loading is working
Notice that we are loading the parent side (the “1” in the “1-to-many”)
You can see the lazy loading in process. If lazy loading works correctly, you should see 2 queries:
- The first query is for loading “Blogs”
- You can see the “select” statement below
Purpose of Screen: To illustrate lazy loading. Notice we are loading records from the “Blogs” table. |
Lazy Loading is working
The Posts table is getting loaded
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.
Conclusions
ADO.NET Entity Framework
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.
Comments
- Anonymous
February 10, 2011
The link to Rob Bagby’s blog is broken.