Monday, November 6, 2017

Entity Framework : Eager Loading of Multiple Levels of Entities by Using Include

This blog will demonstrates how to do multiple levels of entities loading by using include() method in entity framework and there are two ways to load the related entities data
  1.       Eagerly Loading
  2.       Lazy Loading


Eagerly Loading : 
In eager loading process, the query related to other related entities is part of the main query and it will be achieved by using INCLUDE method.

Example :

List<Customer> customers = _db.Customers.Include("Addresses").ToList();

OR

List<CustomerData> customers = _db.Customers.Include(c => c.Addresses).ToList();

as you can see in above ef query, Customer entities will be loaded along with included thier assciated Addresses entity data

in below exmaple you can see the multiple level of entities is being loaded. Customer enetity is being loaded with included entities Addresses, Order and it's releated Review entities.

Example :

List<Customer> customers = _db.Customers. Include("Addresses").Include("Orders.Reviews").ToList();

Lazy Loading:
In lazy loading process, the related entities is automatically loaded from the database the first time that property is being referenced or used



Thanks for visiting!!

1 comment:

Ducathub said...

Very nice blog keep sharing such informative conents.

best dot net training

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...