Entity Framework Core performance improvement tips

By | November 19, 2017

Entity Framework is a popular ORM choice for data access needs in .Net world. It is superior and mature framework. Of course, ORM is an abstraction at the end of day, some times we may feel we are not enough juice out of it when compared with ADO.Net library.

There is always a trade off. Do you want structured, strongly typed query syntax rather or a raw magic string query syntax embedded in code which offers better performance? It boils down to flexibility vs performance. However, it can be possible to achieve some kind of balance which offers best of both worlds.

We collected few tips which can offer better performance from EF

1. Disable Tracking for read queries

By default, EF Core queries that return entities are tracking which means Entity Framework will persist to database any changes detected in the entity during SaveChanges(). In read queries, disabling tracking can offer better query performance because there is no tracking overhead. Good news is change tracking can be disabled at multiple levels based on need and code arrangement.

  • Query level
  • Context Instance level
  • Global level

At Global level, we can disable tracking as shown below

Most of times, we’re dealt with CRUD repos. In such instances, we can choose to disable tracking at query level.

You can also change the default tracking behavior at context instance level

2. Raw SQL queries or Dapper ORM

Nothing can beat raw SQL query performance. After all, EF is built on top of ADO.Net library.  EF has it’s own pros and cons. Too often, performance is cited as a con. Of course, there are alternatives. EF offers one extension method FromSql which offers one way to plug raw sql directly.

Dapper is a simple micro-ORM nuget available as a alternative for heavy duty queries. It provides total control over query composition, raw performance, lot of features (Async support, direct binding with POCO classes, multiple query support).  Of course, we saw some issues with Dapper.

  • Many-to-Many relationships are little tricky to map in Dapper.
  • Biggest issue will be managing complex magic string queries in code. It feels like we’re going back in time!!
  • You lose strong type check with queries.

Overall, we recommend Dapper for only complex object graphs where performance is critical. Light weight object queries are better off with EF

3. Loading data (Early loading vs Deferred loading)

Early loading or Deferred loading has their own merits and place of application. They exist for a reason. Here’s comes the time for you to wear a creative hat. Composing a well defined domain models or entities which can truly reflect your application needs can go a long way adding value to your performance goals indirectly.

Deferred (Lazy) loading related navigation properties sounds great for small applications. But, they’re evil for large applications. They trigger too many round trips which can unnecessarily impact application performance. No wonder, Lazy loading is not yet supported in EF Core.

Prefer early loading navigation properties. Include and ThenInclude lets you drill into nested object graphs to early load related entities.

4. Asynchronous Queries

Entity Framework offers alternate Async API which can be useful to increase throughput from a front end perspective. Threads can be freed up to service other requests while database operation completes. Of course, we admit this is not directly related to EF performance topic we’re covering. Having a well-defined Async call stack all the way from front end to database can greatly improve overall application responsiveness.

5. IQueryable<T> vs IEnumerable<T>

EF Core provides IQueryable<T> implementation. IQueryable<T> allows you to execute a query against data source in it’s entirety. IQueryable<T> represents a expression tree which translates to SQL query at run time. IQueryable<T> helps you to filter, project results directly on remote database rather than in-memory IEnumerable<T> delegates which badly impacts your query performance.