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
- Eagerly Loading
- 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.
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 :
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
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:
Very nice blog keep sharing such informative conents.
best dot net training
Post a Comment