๐ 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:
- Fetch data from the database
- Modify it in memory
- 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();
โ 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")
);
๐ 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
๐ 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!
Top comments (0)