Entity Framework Core Mastery

Batch Updates and Deletes (.NET 7+)

1 Views Updated 5/4/2026

Batch Updates and Deletes (.NET 7+)

Before .NET 7, Entity Framework Core was notoriously terrible at bulk operations. If you wanted to delete 10,000 expired logs, you had to write a LINQ query to download all 10,000 logs into C# memory, track them, mark them for deletion, and wait while EF Core fired 10,000 individual DELETE statements. With the introduction of ExecuteUpdateAsync and ExecuteDeleteAsync, EF Core finally processes bulk operations natively and instantly.

1. ExecuteDeleteAsync (Bulk Deletion)

Instead of downloading the data, you write a standard LINQ Where clause and append the new execution method. This translates into a single SQL statement that executes immediately, bypassing the ChangeTracker entirely.

// Old Way (Crashes your server's RAM): 
// var oldData = _context.Logs.Where(l => l.Year < 2020).ToList();
// _context.Logs.RemoveRange(oldData);
// await _context.SaveChangesAsync();

// New Way (.NET 7+):
// Instantly fires: DELETE FROM Logs WHERE Year < 2020
int deletedCount = await _context.Logs
    .Where(l => l.Year < 2020)
    .ExecuteDeleteAsync();

Console.WriteLine($"Surgically destroyed {deletedCount} rows in 2 milliseconds.");

2. ExecuteUpdateAsync (Bulk Updating)

You can perform massive mathematical updates across millions of rows directly inside the SQL engine utilizing lambda expressions.

// Scenario: Give all Active Employees a 5% raise.
// Instantly fires: UPDATE Employees SET Salary = Salary * 1.05 WHERE Status = 'Active'

int updatedCount = await _context.Employees
    .Where(e => e.Status == "Active")
    .ExecuteUpdateAsync(setter => setter
        // The property to change , The mathematical SQL logic to apply
        .SetProperty(e => e.Salary, e => e.Salary * 1.05m)
        // You can chain multiple properties!
        .SetProperty(e => e.LastUpdated, DateTime.UtcNow) 
    );

3. The ChangeTracker Bypass Danger

Because these bulk methods execute immediately against the database, they DO NOT interact with the ChangeTracker, and they DO NOT wait for SaveChanges().

Warning: If you have a tracked Employee object with ID=5 loaded into your web request's RAM, and in the next line of code you use ExecuteUpdateAsync to fire them... your C# object in RAM will still show them as "Active". The ChangeTracker is not notified of the bulk update! You will have stale data.

4. Interview Mastery

Q: "If `ExecuteDeleteAsync` and `ExecuteUpdateAsync` are significantly faster than `SaveChanges()`, why wouldn't we just abandon the Change Tracker altogether and use these methods for every single CUD operation in our architecture?"

Architect Answer: "While bulk execution methods are phenomenally fast for set-based operations (affecting thousands of rows), they are detrimental when applied to complex Domain-Driven workflows. The ChangeTracker provides incredible benefits: It automatically orchestrates complex object graphs (inserting an Order and its 5 Line Items inside a single transaction), handles Concurrency Token checking to prevent multi-user overwrites, and executes essential interceptors (like auditing who changed the data). `ExecuteUpdateAsync` blindly executes the SQL, completely skipping all EF Core Interceptors, Triggers, and Concurrency validations. We use `SaveChanges` for complex business entity logic, and `ExecuteUpdate` exclusively for raw data janitorial tasks."

Entity Framework Core Mastery
1. Foundations & Architecture
Introduction to Object Relational Mapping (ORM) Entity Framework Core Architecture & Providers Setup and DbContext Integration Code-First vs Database-First Approaches Reverse Engineering Existing Databases (Scaffolding)
2. Code-First Modeling
Entity Conventions & Data Annotations The Fluent API Deep Dive (OnModelCreating) Primary Keys, Composite Keys, & Guids Required Properties & Database Defaults Value Conversions (Enums & Strongly Typed IDs)
3. Relational Architecture
One-to-Many Relationships & Foreign Keys One-to-One Relationships (Dependent Entities) Many-to-Many Relationships & Navigation Properties Owned Entity Types (Value Objects) Table-per-Hierarchy (TPH) Inheritance
4. Data Querying & LINQ
Basic LINQ Queries & IQueryable Execution Tracking vs No-Tracking Queries (Performance) Eager Loading vs Explicit Loading (Include) Lazy Loading Pitfalls & Proxies Client vs Server Evaluation Parsing
5. Manipulating Data (CUD)
Adding, Updating, and Removing Entities The ChangeTracker and Entity States Disconnected Entities in Web APIs Batch Updates and Deletes (.NET 7+)
6. Advanced Performance & Scale
Concurrency Tokens and Optimistic Locking Raw SQL Queries and Views (FromSqlRaw) Compiled Queries for High Throughput Interceptors (Logging & Auditing Data Changes) DbContext Pooling Mechanisms Managing Complex EF Core Migrations (CI/CD)