views:

349

answers:

1

It seems that lazy loading is enabled by default in EF4. At least, in my project, I can see that the value of

dataContext.ContextOptions.LazyLoadingEnabled

is true by default. I don't want lazy loading and I don't want to have to write:

dataContext.ContextOptions.LazyLoadingEnabled = false;

each time I get a new context. So is there a way to turn it off by default, say, across the whole project?

+4  A: 

The edmx file has in the ConceptualModel and EntityContainer definition an attribute for lazy loading where you can set lazy loading generally to false:

<EntityContainer Name="MyEntitiesContext" annotation:LazyLoadingEnabled="false">

This creates the following setting in the ObjectContext constructor:

public MyEntitiesContext() : base("name=MyEntitiesContext", "MyEntitiesContext")
{
    this.ContextOptions.LazyLoadingEnabled = false;
    OnContextCreated();
}
Slauma
Perfect, thanks.
Mikey Cee