There are two changes to the lazy loading feature introduced in the Beta 1 version of Entity Framework.
The first is that the property, ObjectContext.ContextOptions.DeferredLoadingEnabled has been changed to ObjectContext.ContextOptions.LazyLoadingEnabled.
This is a breaking change from Beta 1 and you’ll need to update references to this property.
The bigger change is that LazyLoadingEnabled is true by default for new models.
What exactly does this mean?
When you use the EDM Wizard to create a new model from an existing database, a new annotation is placed in the ENtityContainer element in the CSDL.
<EntityContainer Name="MyEntities" annotation:LazyLoadingEnabled="true">
Annotations in CSDL are code generator directives. The result of this, when using the default code generation template, is that the constructors for the EntityContainer will set LazyLoadingEnabled=true.
Here is one of the three default constructors created for the container
public MyEntities() : base("name=MyEntities", "MyEntities")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
Now whenever you instantiate a new MyEntities context, by default lazy loading is on.
So if you query for customers and do not eager load (Customer.Include(“Orders”)) or project (…select c, c.Orders…) related orders, you will not have to do anything more than ask for them (e.g., mycust.Orders.Count()) to fire off a behind the scenes database query to retrieve the related data.
If you explicitly set LazyLoadingEnabled=false, then you are required to explicitly load related data as with ENtity Framework version 1.
What about old models?
If you have a model from before Beta 2 and are bringing it into this version and you want it to have lazy loading on by default, just open the model up in the XML editor and add the annotation in manually.



