DEV Community

Birupakhya Dash
Birupakhya Dash

Posted on

๐Ÿš€ EF Core Just Changed the Way We Update Data โ€” Hereโ€™s Why It Matters

๐Ÿš€ EF Core Just Changed the Way We Update Data โ€” Hereโ€™s Why It Matters

For years, updating records in Entity Framework followed a familiar pattern:

  1. Fetch data from the database
  2. Modify it in memory
  3. Call SaveChanges()

It workedโ€”but it wasnโ€™t efficient.


๐Ÿง  The Problem with the Old Approach

var data = await _context.FtpRequestDetails
    .Where(x => x.FtpRequestId == ftpRequestId)
    .ToListAsync();

foreach (var item in data)
{
    item.FtpStatusId = 3;
}

await _context.SaveChangesAsync();
Enter fullscreen mode Exit fullscreen mode

โŒ Issues:

  • Loads data into memory unnecessarily
  • Slower for large datasets
  • Requires change tracking
  • More boilerplate code

โšก The Game-Changer: ExecuteUpdateAsync() (EF Core 7+)

EF Core introduced a set-based update approach that directly executes SQL without loading entities.

await _context.FtpRequestDetails
    .Where(x => x.FtpRequestId == ftpRequestId)
    .ExecuteUpdateAsync(setters => setters
        .SetProperty(x => x.FtpStatusId, 3)
        .SetProperty(x => x.ThreadLocked, (bool?)null)
        .SetProperty(x => x.ThreadUnlockedTime, DateTime.Now)
        .SetProperty(x => x.Comments, "Rejected Details")
    );
Enter fullscreen mode Exit fullscreen mode

๐Ÿ” Whatโ€™s Happening Behind the Scenes?

This LINQ query is translated into a single SQL statement:

UPDATE FtpRequestDetails
SET 
    FtpStatusId = 3,
    ThreadLocked = NULL,
    ThreadUnlockedTime = GETDATE(),
    Comments = 'Rejected Details'
WHERE FtpRequestId = @ftpRequestId
Enter fullscreen mode Exit fullscreen mode

๐Ÿ‘‰ No entity loading
๐Ÿ‘‰ No loops
๐Ÿ‘‰ Just pure SQL execution


๐Ÿ’ก Why This Matters

๐Ÿš€ Performance Boost

  • Eliminates unnecessary data fetching
  • Executes directly in the database
  • Ideal for bulk operations

๐Ÿงน Cleaner Code

  • No loops
  • No manual tracking
  • Minimal code, maximum impact

โš™๏ธ Better Scalability

  • Handles large datasets efficiently
  • Perfect for background jobs & batch updates

โš ๏ธ Things to Keep in Mind

โŒ No Change Tracking

EF Core wonโ€™t track these changes.

โŒ No SaveChanges()

This executes immediatelyโ€”no unit of work involved.

โŒ No Entity Events

Hooks, validations, or domain events wonโ€™t run.


๐Ÿงญ When Should You Use It?

โœ” Bulk updates
โœ” Background processing
โœ” Performance-critical operations
โœ” Updating multiple records at once


๐Ÿšซ When NOT to Use It

โŒ If you rely on:

  • Business logic per entity
  • Validation rules
  • Domain events or auditing

๐Ÿ†š Old vs New

Feature Old Approach New Approach
Performance โŒ Slower โœ… Faster
Memory Usage โŒ High โœ… Low
Change Tracking โœ… Yes โŒ No
Code Complexity โŒ More โœ… Less

๐Ÿ Final Thoughts

EF Coreโ€™s ExecuteUpdateAsync() is a major shift toward modern, high-performance data handling.

It brings the power of SQL directly into LINQโ€”without sacrificing readability.

๐Ÿ‘‰ If you're still using the old pattern for bulk updates, it's time to upgrade.


๐Ÿ’ฌ Have you started using ExecuteUpdateAsync() yet?
Would love to hear your experience!

DotNet #EFCore #CleanCode #Performance #SoftwareEngineering #CSharp #Developers

Top comments (0)