Thursday, January 17, 2013

Eager Loading Entity Objects

To eager load an entity object, all we need to use is key word Include. By using Include() method, we are telling entity framework to get a particular object collection when we query for the parent object. Irrespective of lazy loading is turned on or off in the context, eager loading works by just using the Include key word in the entity loading query.

public IQueryable<MyEntity> GetMyEntities()
{
       return this.MyEntities.Include(e => e.SubObjectCollection);
}
In the above example, SubObjectCollection is loaded as soon as the MyEntities loaded. The process of using Include in the query so that we could load a sub object of an entity when it's loaded is called eager loading.



No comments:

Post a Comment