Entity Framework 6: Disable Lazy Loading and specifically load included tables
That is callled eager loading you want to achieve.
var customerNotes = newContext.CustomerNotes.Include(t=> t.Node).ToList();
This should work, i don't really understand the keyword syntax.
If the code above doesn't work try this:
var customerNotes = newContext.CustomerNotes.Include(t=> t.Node).Select(t=> new {
Node = t.Node,
Item = t
}).ToList();
Disable lazy loading by default in Entity Framework 4
The following answer refers to Database-First or Model-First workflow (the only two workflows that were available with Entity Framework (version <= 4.0) when the question was asked). If you are using Code-First workflow (which is available since EF version >= 4.1) proceed to ssmith's answer to this question for a correct solution.
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();
}
My example is not meant that way that the generated ObjectContext
(or DbContext
in newer EF versions) should be edited manually (which would be overwritten with every model update from the database, as ctorx pointed out) but that the EntityContainer
element in the edmx:ConceptualModels
section of the EDMX file should be edited by adding the annotation:LazyLoadingEnabled="false"
attribute - either manually in an XML editor or on the properties page of the designer surface where this option is available as well, Right-Click EDMX then Properties.
This modification of the EDMX file will automatically generate the context class with the disabled lazy loading option in the constructor like shown above. The EDMX file modification itself does not get overwritten when the model is updated from the database.
EF Core enable Lazy Loading conditionally
You can use ChangeTracker.LazyLoadingEnabled
property:
Gets or sets a value indicating whether navigation properties for tracked entities will be loaded on first access.
The default value is true.
e.g.
context.ChangeTracker.LazyLoadingEnabled = false;
var query = context.Set<…>()...;
Related Topics
How to Save Picturebox.Image to File
Sqldatasourceenumerator.Instance.Getdatasources() Does Not Locate Local SQL Server 2008 Instance
Foreach Loop, Determine Which Is the Last Iteration of the Loop
Difference Between Lookup() and Dictionary(Of List())
How to Make a Background Worker Thread Set to Single Thread Apartment
How to Use Httpwebrequest to Pull Image from Website to Local File
Generate N Random and Unique Numbers Within a Range
Does Garbage Collection Run During Debug
How Math.Pow (And So On) Actually Works
End of Central Directory Record Could Not Be Found
How to Target Attributes for a Record Class
Linq to Entities Doesn't Recognize a Method
Minimal and Correct Way to Map One-To-Many with Nhibernate
Windows 10 Scrollintoview() Is Not Scrolling to the Items in the Middle of a Listview
C# - Volatile Keyword Usage VS Lock