Álvaro García
Álvaro García

Reputation: 19386

how to disabled lazy loading for every instance of the dbContext?

I have created an edmx and in the designer.cs file I have 3 constructors. In each constructor I add the followiing line:

this.Configuration.LazyLoadingEnabled = false;

However when I create a new DBContext the lazy loading is enabled because when I create a new DBContext is not used any of this constructors.

Which constructors is used to create a new DBContext?

EDIT: I am not using code first. I create my edmc from a SQL Server database.

Thanks.

Upvotes: 3

Views: 1113

Answers (3)

StuartLC
StuartLC

Reputation: 107317

The property on the edmx design properties is called LazyLoadingEnabled - it defaults to true.

This is used in the T4 template (MyModel.Context.tt) as follows:

public <#=Code.Escape(container)#>()
    : base("name=<#=container.Name#>")
{
<#
    WriteLazyLoadingEnabled(container);
#>
}

Which will write out the following if the property is disabled:

this.Configuration.LazyLoadingEnabled = false;

If FWR the property isn't visible in the EDMX designer, you can remove the conditional code generation, and hard code it:

public <#=Code.Escape(container)#>()
    : base("name=<#=container.Name#>")
{
    this.Configuration.LazyLoadingEnabled = false;
    // more setup here, e.g. this.Configuration.ProxyCreationEnabled = false;    
}

Upvotes: 1

sexta13
sexta13

Reputation: 1568

You can go to the EDMX file, properties, and there's a property Lazy Loading. Just put it to false.

Regards,

==============EDIT===============

Don't you have this? enter image description here

==new EDIT== enter image description here

Upvotes: 3

Eoin Campbell
Eoin Campbell

Reputation: 44308

I think there's a property on the EDMX design time canvas properties to disable lazy loading. 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">

ref: Disable lazy loading by default in Entity Framework 4

Upvotes: 1

Related Questions