I have read this tutorial and this article but I don't understand exactly the use of each loading type.
I Explain
I have this POCO :
public partial class dpc_gestion
{
public dpc_gestion()
{
this.ass_reunion_participant = new HashSet<ass_reunion_participant>();
this.dpc_participant = new HashSet<dpc_participant>();
this.dpc_reunion = new HashSet<dpc_reunion>();
}
public int dpc_id_pk { get; set; }
public Nullable<int> dpc_id_gdp_fk { get; set; }
public Nullable<int> dpc_id_theme { get; set; }
public int dpc_id_animateur_fk { get; set; }
public Nullable<System.DateTime> dpc_date_creation { get; set; }
public Nullable<System.DateTime> dpc_date_fin { get; set; }
public Nullable<System.DateTime> dpc_date_engag_anim { get; set; }
public Nullable<bool> dpc_flg_let_engag_anim { get; set; }
public Nullable<bool> dpc_flg_fsoins_anim { get; set; }
public virtual ICollection<ass_reunion_participant> ass_reunion_participant { get; set; }
public virtual theme_dpc theme_dpc { get; set; }
public virtual gdp_groupe_de_pair gdp_groupe_de_pair { get; set; }
public virtual ICollection<dpc_participant> dpc_participant { get; set; }
public virtual ICollection<dpc_reunion> dpc_reunion { get; set; }
}
I understood this :
For Lazy loading : because the load is lazy, if I call the dbset
dpc_gestion
all navigation properties won't be loaded. This type of loading is the best in performance and responsiveness. It is enabled by default and If I'd like to re-enable it I have to set :context.Configuration.ProxyCreationEnabled = true; context.Configuration.LazyLoadingEnabled = true;
For the eager loading It is not lazy: it loaded all navigation properties when I load
dpc_gestion
. The navigation properties can be loaded usinginclude
method. To enable this loading type :context.Configuration.LazyLoadingEnabled = false;
For the explicit loading It is like the eager loading but we use
Load
method instead ofinclude
.
So I'd like to know :
- If this small resume is true ?
- If it is true, what is the difference between eager and explicit loading?
- If I Use lazy loading and I call for example
dpc_gestion.dpc_participant
, does the navigation properties loads?or I will get an exception? - Is there a case when eager loading or explicit loading were better than lazy loading in performance and responsiveness?
Thanks
Yes.
Eager loading is the opposite of Lazy loading but Explicit loading is similar to lazy loading, except that: you explicitly retrieve the related data in code; it doesn't happen automatically when you access a navigation property. You load related data manually by getting the object state manager entry for an entity and calling the
Collection.Load
method for collections or theReference.Load
method for properties that hold a single entity.From techblog:
And:
You don't get any exception and the navigation properties should loads.
Eager loading is typically more efficient when you need the related data for all retrieved rows of the primary table. And also when relations are not too much, eager loading will be good practice to reduce further queries on server. But when you know that you will not need a property instantly then lazy loading maybe a good choice. And also eager loading is a good choice in a situation where your db context would be disposed and lazy loading could not take place anymore. For example consider the following:
After calling this method, You cannot load the related entity lazily because the
db
is disposed and so the Eager Loading would be a better choice here.One more thing to note is: Lazy loading will produce several SQL request while Eager loading load data with one request. Eager loading is also a good choice to solve the n+1 selects issue in ORMs. Have a look at this post: What is the n+1 selects issue?
Here you will learn how to load related entities in an entity graph explicitly. Explicit loading is valid in EF 6 and EF Core both.
Even with lazy loading disabled (in EF 6), it is still possible to lazily load related entities, but it must be done with an explicit call. Use the
Load()
method to load related entities explicitly. Consider the following example.In the above example,
context.Entry(student).Reference(s => s.StudentAddress).Load()
loads theStudentAddress
entity. TheReference()
method is used to get an object of the specified reference navigation property and theLoad()
method loads it explicitly.In the same way,
context.Entry(student).Collection(s => s.Courses).Load()
loads the collection navigation property Courses of the Student entity. TheCollection()
method gets an object that represents the collection navigation property.The
Load()
method executes the SQL query in the database to get the data and fill up the specified reference or collection property in the memory, as shown below.Query(): You can also write LINQ-to-Entities queries to filter the related data before loading. The Query() method enables us to write further LINQ queries for the related entities to filter out related data.
In the above example,
.Collection(s => s.StudentCourses).Query()
allows us to write further queries for theStudentCourses
entity.Question 1 and 2:
Your explanation of lazy loading and eager loading is correct.
The use of explicit loading is a bit different than you described.
EntityFramework
returnsIQueryable
objects, which essentially contain the query to the database. But these are not executed until the first time they are enumerated.Load
executes the query so that its results are stored locally.Calling
Load
is the same as callingToList
and throwing away thatList
, without having the overhead of creating theList
.Question 3:
If you use lazy loading,
EntityFramework
will take care of loading the navigation property for you, so you won't get an exception.Keep in mind that this can take a while and make you application unresponsive.
Question 4:
In disconnected cases (e.g. network application) you can't use lazy loading, because these objects are translated into DTOs and then not tracked by
EntityFramework
.Also, if you know you're going to use a navigation property, its good practice to load it eagerly, so you don't have to wait until they are loaded from the database.
For example, lets say you store the result in a list and bind it to a WPF DataGrid. If the DataGrid accesses a property that is not loaded yet, the user experiences a noticeable timeout until that property is displayed. Additionally the application will not respond during the loading time (if you dont load asynchronously).