Disable default lazy loading in Entity Framework 4

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

dataContext.ContextOptions.LazyLoadingEnabled 

true by default. I do not need lazy loading, and I do not want to write:

 dataContext.ContextOptions.LazyLoadingEnabled = false; 

every time I get a new context. So is there a way to disable it by default, say throughout the project?

+54
entity-framework entity-framework-4 lazy-loading
Jun 03 '10 at 15:08
source share
5 answers

The following answer relates to a Database-First or Model-First workflow (only the two workflows that were available with the Entity Framework (version <= 4.0) when the question was asked). If you are using the Code-First workflow (which is available from version EF> = 4.1), go to ssmith answer to this question for the right solution.




The edmx file has an attribute for lazy loading in the definition of <ConceptualModel> and <EntityContainer> , where you can set the lazy load as a whole to false:

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

The following parameter is created in the ObjectContext constructor:

 public MyEntitiesContext() : base("name=MyEntitiesContext", "MyEntitiesContext") { this.ContextOptions.LazyLoadingEnabled = false; OnContextCreated(); } 

My example does not mean that the generated ObjectContext (or DbContext in newer versions of EF) needs to be edited manually (which will be replaced by each update of the model from the database, as ctorx noted), but this is the EntityContainer element in the edmx:ConceptualModels section of the edmx:ConceptualModels file should be edited. adding the annotation:LazyLoadingEnabled="false" attribute annotation:LazyLoadingEnabled="false" - either manually in the XML editor or on the constructor surface properties page, where this parameter is also available. This modification of the EDMX file automatically generates a context class with disabled lazy loading in the constructor, as shown above. Modification of the EDMX file itself is not overwritten when the model is updated from the database.

+62
Jun 03 '10 at 15:51
source share

I wrote a quick example showing how the new Lazy Loading features work with EF Code First . Achieving what you want in the first code model is just a matter of adding one line to the DbContext constructor, for example:

 public BlogContext() { this.Configuration.LazyLoadingEnabled = false; } 
+58
Oct 03 2018-11-11T00:
source share

If you are probably using EF4 Code First, right? So, in Initializing your context there is an override of "OnModelCreated".

In this method, I just called and set the property, and everything was resolved.

 protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.Configuration.LazyLoadingEnabled = false; } 

My model is now much nicer. Lazy loading is great ... but not when you don't want it. And when you start having circular links, it's just ridiculous.

+24
Mar 24 '13 at 23:20
source share

You can also do this from the designer. Just open the .edmx file, right-click anywhere in the model, and select Properties. Then set the LazyLoadingEnabled parameter to false. enter image description here

+20
May 30 '13 at 5:35
source share

If you are first modeling the code, simply remove the virtual in the link / object properties. Having a virtual link will allow LazyLoading to follow this specific link.

+3
Jul 07 '13 at 14:40
source share



All Articles