Entity Framework Performance Tips

Sercan Selçuk
3 min readApr 11, 2019

Entity framework maybe most popular ORM in these day. With Core verison right now it s faster more useful. So many dot net project developing with EF.

Why Entity Framework ?
-
Easy and fast developing
- Less mistake
- Saving time
- More readable codes.

Great! So What’s The Problem
Sometimes in big projects and big database tables our quaries can get slow.
To prepare us this kind of situation we should know some performance tricks about EF.

Deactive LazyLoading

If you are using .net core this properties is coming false in as defult. So you dont need write this code for EF Core

Always Use Async — Await

To increase the performance of our applications specially when we develop an API or some process about I/O, we always prefer async and await. So after set your method as async, dont forget to set your EF queries as async as well

Skip & Take

When you have 1000 products in your table, you don’t need to load and show it all to your client page. Always better to get the data part by part. By skip & take property you can show your data in an optimal range

Using AsNoTracking() Is So Important

Why? Because when you get a data from database by EF, this guy bring your table with a copy to compare any changes happend or not over the data. That s why when you need to aselect a data unless you need change it always bring with AsNoTracking() property . Because when you use AsNoTracking() ef is gonna know that you wont do any changes and it wont bring that copy.

Using Select Is So Important Too

When you need to list your products, if you just need to show picture, name and price of the product you dont need to get it with other details like createdDate. To just bring the necessary columns you should use Select property

Don’t Prefer Contains!

Contains property in EF is a performance killer! This property is equal to WHERE IN command in Sql. Specially when you need to put so many ID values inside contains, you will realize how slow it is.

If you use join rather than contains you will see the difference about query response performance

Using AddRange In Multiple Inserts

When you have a list to insert to your database table it s not a good option add them one by one. This method is harder way to do it. The easier and faster way is to insert all list in one time by AddRange property. Let’s see the difference

Using FirstOrDefault() Rather Then Where

If you want to get one row from table, you don’t need to write your condition inside WHERE. You are going to use FirstOrDefault to get all coloumns of that row, so you can write same condition inside your FirstOrDefault.

Result

We should accept that EF is really cool ORM tool to make easy our jobs. These tips are really can be helpful for your system. Specially if your tables take like 50k or upper request per day. But we should know that EF is not the solution of all problems. If you really have got complicated queries that need to work so many times, you can choose to write your queries with ADO.NET to get more performance or to get best performance of course you can you use Store Procedures..

Thank your reading :)

--

--