03 June 2016

Lazy loading/Deferred loading in Entity frame work

One of the important functions of Entity Framework is lazy loading. Lazy loading means delaying the loading of related data with its parent object, until you specifically request for it.


you can also turn off lazy loading for a particular property or an entire context. To turn off lazy loading for a particular property, do not make it virtual. To turn off lazy loading for all entities in the context, set its configuration property to false:



public partial class SchoolDBEntities : DbContext
{
    public SchoolDBEntities(): base("name=SchoolDBEntities")
    {
        this.Configuration.LazyLoadingEnabled = false;
    }
}

Below are the advantages of lazy loading:




  • Minimizes start up time of the application.

  • Application consumes less memory because of on-demand loading.

  • Unnecessary database SQL execution is avoided.


The only one disadvantage is that the code becomes complicated. As we need to do checks if the loading is needed or not, there is a slight decrease in performance.


 

No comments: