3

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.

Álvaro García
  • 18,114
  • 30
  • 102
  • 193

3 Answers3

3

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

sexta13
  • 1,558
  • 1
  • 11
  • 19
  • I have not this propery in the properties of the edmx. I have only compile action, copy in the result folder, tool for namespace, custom tool, file name and the path. – Álvaro García May 28 '13 at 09:50
  • 2
    @ÁlvaroGarcía You have to click in the designer. What you see are the properties of 'just the file', not the properties of the current active object in the designer. – Maarten May 28 '13 at 09:54
  • 1
    You have to open the edmx, and then press F4 (for properties) – sexta13 May 28 '13 at 09:54
  • The value in the properties of the edmx was true, I changed it to false but when I create a new dbContext, it has the lazy loading to true. – Álvaro García May 28 '13 at 10:01
  • 1
    Did you made a clean solution and rebuild? do you create the dbcontext how? like this: using (SergioEntities dm = new SergioEntities()) { } – sexta13 May 28 '13 at 10:04
  • I delete my edmx and created it again. I select the connection and the databse in the wizzard. When The edmx is created, I change the lazy loading property in the edmx. The designer.cs file has in thethree constructors the lazyLoadingEnabled = false, but this code is never executed. – Álvaro García May 28 '13 at 10:08
  • 1
    and when you put a breakpoint like i did in the image you see lazyloading = true? – sexta13 May 28 '13 at 10:13
  • 1
    Then I don't understand. I have this: using (SergioEntities dm = new SergioEntities()) { bool test = dm.Configuration.LazyLoadingEnabled; } and test gives me false! I have just created a EDMX, add a table, and have put Lazy Loading Enable to false. It works... – sexta13 May 28 '13 at 10:17
  • Thanks. It is very strange. really I don't know what constructor is used to create the dbContext. – Álvaro García May 28 '13 at 10:24
  • 1
    One last question, are you using 4.5 or 4.0? if it is 4.5, you can try this last thing. delete the .tt files under EDMX file and again on properties put the code generation strategy to default. Try it out :) – sexta13 May 28 '13 at 10:34
  • I am using EF 4.0. Well, the library 4.5 for C# 4.0, it could see it as EF 4.4. But the problem persists. Thank again. – Álvaro García May 28 '13 at 11:25
1

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

Community
  • 1
  • 1
Eoin Campbell
  • 43,500
  • 17
  • 101
  • 157
1

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;    
}
StuartLC
  • 104,537
  • 17
  • 209
  • 285