Press ESC to close

How to improve data access performance in EF Core

Entity Framework Core (EF Core) is an open-source ORM framework that automates the interaction between an application’s object model and a database’s data model. It allows NET objects to be employed for database operations, making CRUD tasks easier to execute without understanding the database’s data storage. EF Core also makes entity retrieval, insertion, change, and deletion easy using C#.

You can boost data access efficiency with EF Core in a variety of ways, from eager loading to eliminating the number of database round trips necessary for your queries. In this post, we will look at 10 EF Core tips, tricks, or tactics that we may apply to increase the data access speed of our NET Core apps.

Create a project for a console app using Visual Studio

Follow the instructions below to build a new.NET Core console app project in Visual Studio 2023:

  1. Start the Visual Studio IDE.
  2. Select “Create a new project.”
  3. Select “Console App (.NET Core)” from the selection of templates provided in the “Create new project” dialogue.
  4. Next, click.
  5. Give the new project a name and a location in the “Configure your new project” window.
  6. Next, click.
  7. Select “.NET 7.0 (Standard Term Support)” as the Framework version in the “Additional Information” window that appears.
  8. Click the Create button.

Simply retrieve the information you need

When dealing with large amounts of data, try to obtain only the records needed for the specified query. When retrieving data, use projections to choose only pertinent fields and avoid retrieving irrelevant fields.

The code sample below demonstrates how to extract data in a paged format. Take note of how the initial page index and page size were used to select only the necessary data.

Divide your enormous data context into several smaller data contexts

Your database is represented by the data context in your application. The data context should be divided into multiple smaller data contexts instead of using a single large data context. The startup time of a big data context is a key performance limitation in Entity Framework Core. As a result, the data context must be divided into numerous smaller data contexts.

Ideally, each module or unit of work should only have one data context. Simply build a new class for each data context and extend it from the DbContext class to use multiple data contexts.

For an enormous number of entities, use batch updates

When there is a batch of update statements to be executed, the default behaviour of EF Core is to transmit individual update statements to the database. Multiple database hits, of course, result in severe performance overhead. You may use the UpdateRange() function to adjust this behaviour and improve batch updates.

Block change tracking for read-only queries

EF Core’s default behaviour is to track objects obtained from the database. Tracking is essential when updating an object with new data; however, it is an expensive procedure when dealing with huge data sets. As a result, you may optimise efficiency by removing tracking while you aren’t altering things.

To increase efficiency, use AsNoTracking for read-only queries, i.e., when you wish to get entities without altering them. The following code sample shows how to use AsNoTracking to stop tracking for a single query in EF Core.

Make use of DbContext pooling

By generating and reusing DbContext objects, EF Core provides a DbContext pool technique for improving app efficiency by lowering overhead and memory usage.

Instead of synchronous code, use asynchronous code

You should employ async code to increase your application’s speed and responsiveness.

Reduce the number of database round visits

The N+1 selects problem, which has plagued database performance since the early days of ORMs, has the potential to drastically reduce round trips to the database. This issue happens while trying to import data from two separate tables which have connections. This problem happens in EF Core when the outer for-each loop retrieves all authors with a single query while the inner foreach retrieves the books N times. This issue may be resolved by eagerly loading related data, which includes the book data in the first query for the author data.

Included in EF Core 7, cuts database round trips from N+1 to one, facilitating eager loading and minimising transactions for single insert statements, yielding considerable performance benefits.


Finally, here are the strategies for improving data access speed in EF Core, which include fine-tuning database design, indexes, queries, and stored functions. It emphasizes the significance of performance in creating apps and recommends evaluating performance before and after shifts based on individual app requirements.