Lightweight, lazy and explicit loading in EF6

I have read this tutorial and this article but I do not understand exactly the usage of each upload type.

Explanation

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 that:

  • For lazy loading : because the load is lazy, if I call dbset dpc_gestion

    , all navigation properties will not be loaded . This type of download is the best in performance and speed. It is enabled by default and if I would like to enable it again I have to set:

    context.Configuration.ProxyCreationEnabled = true;    
    context.Configuration.LazyLoadingEnabled = true;
    
          

  • For active loading, It is not lazy: it loaded all navigation properties when I load dpc_gestion

    . Navigation properties can be loaded using the method include

    . To enable this boot type:

    context.Configuration.LazyLoadingEnabled = false;
    
          

  • For explicit loading This is similar to eager loading, but we use the Load

    method instead include

    .

So, I would like to know:

  • If this small summary is correct?
  • If this is true, what is the difference between eager and explicit loading?
  • If I am using lazy loading and I call for example dpc_gestion.dpc_participant

    , are the navigation properties loaded? or will i get an exception?
  • Is there a case where heavy loading or explicit loading is better than lazy loading in terms of performance and responsiveness?

thank

+19


source to share


2 answers


If this small summary is correct?

Yes.

If this is true, what is the difference between eager and explicit loading?

Unwanted loading is the opposite of Lazy loading , but Explicit loading is similar to lazy loading , except: you are explicitly extracting associated data into your code; this does not happen automatically when the nav property is accessed. You load related data manually by getting the object state manager entry for an object and calling a method Collection.Load

on collections or a method Reference.Load

on properties that contain a single object.

From techblog :

Lively loading:

Lazy loading is the opposite of Lazy loading, which: The process of loading a specific set of related objects along with the objects that were requested in the request.

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 an explicit request using the Load method on the navigation properties.

and



If I am using lazy loading and I call, for example dpc_gestion.dpc_participant

, are the navigation properties loaded? or will i get an exception?

You won't get any exceptions and the navigation properties should be loaded.

Is there a case where fast loading or explicit loading was better than lazy loading in performance and responsiveness?

Desired loading is usually more efficient when you need related data for all of the retrieved rows of the primary table. Also, when the relationship is not too great, heavy load will be a good practice to reduce further requests on the server. But when you know you won't need real estate instantly, then lazy loading may be a good choice. And also the desired loading is a good choice in a situation where your db context will be removed and lazy loading can no longer be. For example, consider the following:

public List<Auction> GetAuctions()
{
    using (DataContext db = new DataContext())
    {
        return db.Auctions.ToList();
    }
}

      

After calling this method, you cannot lazily load the associated object because it db

is located and therefore Eager Loading would be the best choice here.

One more thing to note: Lazy loading will produce multiple SQL queries, and load will load data in a single query. Desired loading is also a good choice for solving the n + 1 problem in ORM. Take a look at this post: What does problem n + 1 choose?

+17


source


Question 1 and 2:

Your explanation of lazy loading and reliable loading is correct.
Using explicit loading is slightly different from the one described.

EntityFramework

returns IQueryable

objects that essentially contain a database query. But they are not executed until they are listed.
Load

executes the query so that the results are stored locally.
The call is the Load

same as the call ToList

and throws it away List

with no creation overhead List

.

Question 3:



If you are using lazy loading, EntityFramework

will take care of loading the navigation property for you, so you won't get an exception.
Be aware that this may take a while and make your expression inappropriate.

Question 4:

In disabled cases (like a network app), you cannot use lazy loading because these objects are translated into DTOs and then not tracked EntityFramework

.

Also, if you know you are going to use a navigation property , its good practice to load it eagerly , so you don't have to wait for them to load from the database.
For example, let's say you store the result in a list and bind it to a WPF DataGrid. If the DataGrid is accessing a resource that has not yet been loaded, the user experiences a noticeable timeout until this property is displayed. Also, the app will not respond during load (unless you load asynchronously).

+5


source







All Articles