ADO.NET Entity Framework and .NET 4 & SQL Profiler : Lazy Loading of Entities

 

ADO.NET Entity Framework and .NET 4 & SQL Profiler : Lazy Loading of Entities

  • Comments 1

I was recently reading Rob Bagby’s blog about the ADO.NET Entity Framework and lazy loading.

hyperlink

http://www.robbagby.com/entity-framework/is-lazy-loading-in-ef-4-evil-or-the-second-coming/

What this blog post will address:

 

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

hyperlink[4]

http://brunoblogfiles.com/zips/DemoFeatures3.zip

Previous Blog Entries

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 objectsimage
How To Get This Screen:
: We just double clicked on BloggingModel.edmx

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?image
How To Get This Screen:
: Part of source code

 

 

Purpose of Screen: To run SQL Server Profilerimage

How To Get This Screen:
: Click on the Start menu

 

Starting a Profiler Trace 

Start at the “File” menu

  • Choose “New Trace”
Purpose of Screen: To start a New Trace
snap974
How To Get This Screen:
: Click on the File Menu

 

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.
snap975
How To Get This Screen:
: File, New Trace

 

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 screensnap976
How To Get This Screen:
: File, New Trace, and choose a template

 

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
snap977
How To Get This Screen:
: Start Visual Studio

 

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.snap978
How To Get This Screen:
: Start the application

 

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.
snap979
How To Get This Screen:
: You get this screen when you start a new trace

 

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.

Purpose of Screen: To show lazy loading working. Notice that child table query (for Posts) executes separately, once the child records are accessed.
snap980
How To Get This Screen:
: You get this screen when you start a new trace

 

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.

image

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.

Page 1 of 1 (1 items)
Leave a Comment
  • Please add 1 and 8 and type the answer here:
  • Post