Best practices to improve Entity Framework performance

Maximize Entity Framework performance by following the best and recommended practices

Entity Framework performance

Microsoft's Entity Framework is an extended ORM that helps you to isolate the object model of your application from the data model. It is an open source ORM framework for ADO.Net and is included as part of .Net Framework. In this post, I will present a few tips that can be followed to optimize Entity Framework performance. In the sections that follow, I would examine a few tips that can be adopted to improve application performance when you are working with Entity Framework.

Does your Entity Data Model represent a single Unit of Work?

When you create your EDM (Entity Data Model) you should ensure that the EDM represents a single unit of work and not the entire database especially when you have many objects (tables, stored procedures, views, etc.) in your database that are disconnected or are not needed for a particular unit of work. If your EDM represents the entire database when it is not needed, it can degrade the application's performance because of the need of loading many unnecessary objects in the memory. In essence, you should break up a large entity data model into smaller ones with each model representing an unit of work.

You can refer to this MSDN article for more information on how Entity Framework performance can be improved.

Disable Change tracking

You should disable change tracking if it is not needed. Most importantly, you do not need change tracking when you would just want to retrieve data and updates on the data read aren't needed at all. You can use the following statement to disable change tracking or cache the result of a query when you want to retrieve customers from the database without the need of updating the records.

If you want to disable object tracking for the Customers table, you can use the following code.

PayrollContext context = new PayrollContext();

Reduce the cost of view generation using Pre-generated Views

The creation of ObjectContext is a costly operation as it involves the cost of loading and validating the metadata. You should take advantage of pre-generated views to reduce the response time when the first request is executed. In essence, the Entity Framework runtime creates a set of classes (also called the view) when the object context is instantiated for the first time. You can reduce this overhead by pre generating the view for the EDMX file using the EdmGen.exe command line tool or T4 templates. Note that if the schema files of the model has changed, you would need to re-generate the views file by executing the EdmGen.exe with the /mode:ViewGeneration flag. You can also pre-generate views with a code first model.

Disable Auto Detection of Changes

When trying to update the database the Entity Framework needs to know the changes that have been made to an entity from the time it was loaded in memory. This change detection is done by comparing the old values of the properties with the new or changed values when you make a call to the methods like, Find(), Remove(), Add(), Attach() and SaveChanges() methods. This change detection is very costly and can degrade the application's performance primarily when you are working with many entities. You can disable change detection using the following code.

When you want to disable change detection, it is a good practice to disable it inside a try/catch block and then re-enable it inside the finally block. Note that you can ignore this when you are working with a relatively small set of data - you would get significant performance gains by turning change detection off when you are working with a large set of data.

Other points to keep in mind

Use projections to select only the fields that are needed when retrieving data. You should avoid retrieving fields that are not needed.

The following code snippet illustrates how you can use retrieve data in a paged manner - note how the starting page index and page size has been used to select only the data that is needed.

int pageSize = 25, startingPageIndex = 1;

NorthwindEntities dataContext = new NorthwindEntities();

List lstCus = dataContext.tblCustomers.Take(pageSize)

.Skip(startingPageIndex * pageSize)


You should also select the appropriate collection and use compiled queries to improve performance of your LINQ queries when retrieving data exposed by the EDM. Avoid pulling all the database objects in one entity data model. Retrieve only the number of records that are needed and avoid using "Contains" when using LINQ to Entities. You can use paging to retrieve only the data that is requested for or restrict the amount of data that is retrieved from the database. Also, you should add indexes to your entities by making a call to the CreateIndex() method.

You can learn more on the performance considerations when using Entity Framework from this link.

Copyright © 2015 IDG Communications, Inc.

InfoWorld Technology of the Year Awards 2023. Now open for entries!