Opting out of object tracking can speed up read scenarios like read-only endpoints on an HTTP API. There is a potential risk of using the AsNoTracking feature. While it reduces the cost of processing entities for object tracking, it can add to memory overhead. The mismatch between the assumed behavior of the engine and actual behavior can lead us to introduce bugs.
Using monitoring software, we’ll notice that there are strange outliers in performance, likely due to the execution strategy compensating for a failed connection or SQL timeout. Sometimes we can fix these issues through infrastructure management or by optimizing queries to be less taxing on our database. While it may look like we executed a single query, as we loop through our Movie entity, we are making an additional query for each navigation property. For every Movie (N), we are making a separate call to the database for every related Character entity (+1).
Use batch updates for large numbers of entities
As per the above figure, Entity Framework fits between the business entities (domain classes) and the database. It saves data stored in the properties of business entities and also retrieves data from the database and converts it to business entities objects automatically. It’s easy to forget the scale of the data we’ll be dealing with in production during development. Additionally, as our application grows and we add features, our query needs will change. Most database engines will add indexes to primary and foreign keys, but we may also have columns that provide other partitioning opportunities. The aforementioned changes concern only Microsoft SQL Server (including Azure SQL Database).
When dealing with massive volumes of data, you should strive to retrieve only the required records for the specific query. When fetching data, you should use projections to pick just the required fields and avoid retrieving unnecessary fields. Choose the provider package for the database that we want to access. In this case, select Microsoft.EntityFrameworkCore.SqlServer for MS SQL Server. We have to be sure that it has .NET symbol, and the author is Microsoft.
What is Entity Framework Core?
Note that we’re using an in-memory database here for simplicity. The following code snippet illustrates how you can use async code to update an entity in the database using EF Core. Using a DbContext pool in EF Core can improve performance by reducing the overhead involved in building and disposing of DbContext objects. Ideally, you should only have one data context per module or unit of work. To use multiple data contexts, simply create a new class for each data context and extend it from the DbContext class.
The code snippet given below shows how you can retrieve entities directly from the database for read-only purposes, without tracking and without loading them into the memory. Entity Framework Core allows us to access the database via the provider model. There are different Entity Framework Core DB providers available for the various databases. Entity Framework Core is a lightweight, extensible what is entity framework and cross-platform version of Microsoft’s object-relational mapper (ORM), Entity Framework is the official data access platform for Microsoft. My architecture comprises a PostgreSQL Pgpool server orchestrated via the Bitnami Postgresql HA Helm Chart. The primary goal is to establish a connection using Entity Framework Core and perform migrations to set up the initial data structure.
Native SQL
EF Core optimizes queries by caching LINQ Expressions, but first, it has to compile our LINQ expressions. This processing of LINQ expressions is noticeable in production environments. It’s worth experimenting with AsSplitQuery, as it can speed up queries and reduce network traffic by removing data duplication. If not explicitly told to use the same column, EF Core will create a duplicate column for NumberOfDoors. We can leave out the To argument, as we can imply that we want to generate up to the latest migration.
You can improve data access performance in EF Core in many different ways, ranging from using eager loading to reducing the database round trips required by your queries. In this article, we will explore 10 tips and tricks or strategies we can use in EF Core to improve the data access performance of our .NET Core applications. You would NOT have to change your code even a bit, as EFCore takes care of data access. This is how you build decoupled applications the right way.
Moreover, while IQueryable supports query optimizations and lazy loading, IEnumerable does not. This explains why IQueryable executes queries faster than IEnumerable. For read-only queries, i.e., when you want to retrieve entities without modifying them, you should use AsNoTracking to improve performance. The following code snippet illustrates how AsNoTracking can be used to disable tracking for an individual query in EF Core. The data context in your application represents your database.
- When dealing with massive volumes of data, you should strive to retrieve only the required records for the specific query.
- The EF Core query abstraction is a leaky one, with many of the database concepts finding their way into the interface.
- EF Core is intended to be used with .NET Core applications.
It’s highly advised not to enable this feature, as navigation properties become opaque mechanisms that trigger unnecessary and costly roundtrips to the database. We must specify the appropriate database provider when scaffolding so that the scaffolding can respect the SQL syntax of our database. This command will create a new .config directory with a dotnet-tools.json file.