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 using include
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 of include
.
So I'd like to know :
dpc_gestion.dpc_participant
, does the navigation properties loads?or I will get an exception?Thanks
Explicit loading means that the related data is explicitly loaded from the database at a later time. Lazy loading means that the related data is transparently loaded from the database when the navigation property is accessed.
Lazy loading in Entity Framework is the default phenomenon that happens for loading and accessing the related entities. However, eager loading is referred to the practice of force-loading all these relations.
Eager Loading: used to load core modules and feature modules that are required to start the application. Pre-Loading: used to load specific feature modules that are very likely to be used soon after the application started. Lazy Loading: all other modules could be lazily loaded on demand after the application started.
Eager Loading is a design pattern in which data initialization occurs on the spot. Lazy Loading is a design pattern that we use to defer initialization of an object as long as it's possible.
If this small resume is true ?
Yes.
If it is true, what is the difference between eager and explicit loading?
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 the Reference.Load
method for properties that hold a single entity.
From techblog:
Eager Loading :
Eager loading is the opposite of Lazy loading which is : The process of loading a specific set of related objects along with the objects that were explicitly requested in the query.
Explicit Loading :
Explicit loading is defined as : when objects are returned by a query, related objects are not loaded at the same time. By default, they are not loaded until explicitly requested using the Load method on a navigation property.
And:
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?
You don't get any exception and the navigation properties should loads.
Is there a case when eager loading or explicit loading were better than lazy loading in performance and responsiveness?
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:
public List<Auction> GetAuctions() { using (DataContext db = new DataContext()) { return db.Auctions.ToList(); } }
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?
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
returns IQueryable
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 calling ToList
and throwing away that List
, without having the overhead of creating the List
.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With