Entity Framework Core 3.1: Performance and LINQ Improvements

EF Core 3.1 finally addresses the major criticism of previous versions: the implicit client-side evaluation of LINQ queries. In versions 1.x and 2.x, if EF couldn’t translate a C# expression to SQL, it would silently fetch ALL rows and filter in memory. This caused massive production outages. EF Core 3.1 breaks this behavior by throwing an exception, prioritizing correctness and performance transparency.

Client-Side Evaluation is Gone

// EF Core 2.2: Runs in memory (Dangerous!)
// SELECT * FROM Orders
var orders = context.Orders
    .Where(o => ParseStatus(o.Status) == "Active") 
    .ToList();

// EF Core 3.1: Throws InvalidOperationException
// Fix: Explicitly move to client
var orders = context.Orders
    .AsEnumerable() // Explicit ownership of memory load
    .Where(o => ParseStatus(o.Status) == "Active")
    .ToList();

Interception API

EF Core 3.1 introduces powerful low-level interception of database operations. Useful for auditing or modifying SQL on the fly.

public class HintInterceptor : DbCommandInterceptor
{
    public override InterceptionResult<DbDataReader> ReaderExecuting(
        DbCommand command, 
        CommandEventData eventData, 
        InterceptionResult<DbDataReader> result)
    {
        command.CommandText += " OPTION (RECOMPILE)";
        return result;
    }
}

// Register in OnConfiguring
optionsBuilder.AddInterceptors(new HintInterceptor());

Cosmos DB Provider

3.1 solidifies the Cosmos DB provider usage, allowing you to use EF patterns against NoSQL.

modelBuilder.Entity<Order>()
    .ToContainer("Orders")
    .HasPartitionKey(o => o.CustomerId);
    
modelBuilder.Entity<Order>()
    .OwnsMany(o => o.Items); // Embedded documents!

Key Takeaways

  • LINQ translation is now robust and strict; fix your client-side eval exceptions immediately.
  • Interceptors allow advanced DBA-style tuning.
  • Cosmos DB support brings familiar unit-of-work patterns to document storage.

Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.