Introduction
LINQ to Entity is a great ORM for querying and managing databases. There are some points that we should consider while designing and querying databases using the Entity Framework ORM.
Disable change tracking for entity if not needed
In the Entity Framework, the context is responsible for tracking changes in entity objects. Whenever we are only reading data, there is no need to track the entity object. We can disable entity tracking using the MergeOption, as in:
AdventureWorksEntities e = new AdventureWorksEntities();
e.EmployeeMasters.MergeOption = System.Data.Objects.MergeOption.NoTracking;
Use Pre-Generating Views to reduce response time for the first request
While working with Entity Framework, view generation may take longer for a large and complicated model. We can use a pre-generated view to save run time cost to generate a view for the first request of a page. A Model Generated view helps us to improve start-up performance at run time.
Avoid fetching all the fields if not required
Avoid fetching all fields from the database that are not really required. For example we have an EmployeeMaster table and for some operation I require only the EmployeeCode field so the query must be:
AdventureWorksEntities e = new AdventureWorksEntities();
string code = e.EmployeeMasters.Where(g => g.EmployeeId == 1000)
.Select(s => s.EmployeeCode).FirstOrDefault();
Retrieve only required number of records (rows)
Do not collect all data while binding data to a data grid. For example if we have a data grid on a page and we only show 10 records on the screen, so do not fetch all records from the database, as in:
AdventureWorksEntities e = new AdventureWorksEntities();
int pageSize = 30, startingPageIndex = 3;
List<EmployeeMaster> lstemp = e.EmployeeMasters
.Take(pageSize)
.Skip(startingPageIndex * pageSize).ToList();
Join the conversation! Your thoughts help the community grow.
Sign in to leave a comment
It is the same account you read, post and publish with — and you will come straight back to this page.

Ketan MokariyaPosted Aug 23, 2017, 3:45 AM
Good article Jignesh sir.
Gowtham RajamanickamPosted Apr 11, 2015, 1:26 PM
informative
Gowtham RajamanickamPosted Apr 11, 2015, 1:26 PM
very nice
Gowtham RajamanickamPosted Apr 11, 2015, 1:26 PM
ery nice
Santosh YadavPosted Apr 12, 2013, 3:22 PM
Very nice article.