Entity Framework Performance Improvements with Code Examples

Introduction

Entity Framework (EF), a .NET ORM that simplifies data access, can become a performance bottleneck. This article explores strategies to optimize EF performance with practical code examples. Ensure efficient EF Core queries for a responsive and scalable application.

Some Tips for Performance Improvements EF in . NET.

1. Implement Caching

// Object caching
var cachedCustomers = _cache.Get<List<Customer>>("customers");
if (cachedCustomers == null)
{
    cachedCustomers = context.Customers.ToList();
    _cache.Set("customers", cachedCustomers, TimeSpan.FromMinutes(5));
}
// Query plan caching
context.Database.SetCommandTimeout(300);
var customers = context.Customers.Where(c => c.City == "New York").ToList();

In the object caching example, we use a cache provider (e.g., Redis, Memcached) to store and retrieve the customer data. This can significantly reduce the number of database calls.

In the query plan caching example, we set the command timeout to 300 seconds, which allows the database to cache the query plan for better performance on subsequent executions.

2. Optimize Tracking

// Disable tracking for read-only data
var customers = context.Customers.AsNoTracking().ToList();
// Disable tracking for specific queries
var customers = context.Customers
    .Where(c => c.City == "New York")
    .AsNoTracking()
    .ToList();

In these examples, we use the AsNoTracking() method to disable change tracking for the customer data, which can improve performance and reduce memory consumption.

3. Use Efficient LINQ Queries

// Projection to minimize data fetched
var customerNames = context.Customers
    .Select(c => c.Name)
    .ToList();
// Eager loading to reduce round trips
var customers = context.Customers
    .Include(c => c.Orders)
    .ToList();

In the first example, we use a projection to fetch only the customer names, which can be more efficient than fetching the entire customer entity.

In the second example, we use eager loading to fetch the customer and their related orders in a single database query, reducing the number of round trips to the database.

4. Reduce data retrieved with LINQ projections

This example retrieves only the Name and Id from the Products table, instead of the entire product objects.

List<ProductDto> products = context.Products
    .Select(p => new ProductDto 
    { 
        Name = p.Name, 
        Id = p.Id 
    })
    .ToList();
public class ProductDto 
{
    public string Name { get; set; }
    public int Id { get; set; }
}

5. Use Stored Procedures for Complex Queries

For complex queries that involve multiple joins and computations, consider using stored procedures.

var users = _context.Users .FromSqlRaw("EXEC GetUsersWithOrders") .ToList();

6. Implement Paging and Filtering

Implement paging to load data in chunks rather than retrieving large datasets at once.

var pageSize = 10;
var pageNumber = 1;
var pagedUsers = _context.Users
                         .Skip((pageNumber - 1) * pageSize)
                         .Take(pageSize)
                         .ToList();

Conclusion

By applying these EF Core performance tips, you can make your applications more efficient and responsive. Each strategy addresses a common performance pitfall, helping you to optimize data access patterns and improve overall application performance.


Similar Articles