© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
1 reply
Kroks

EF-Core: Save one by one or bulk?

if (didAction) {
  user.LastProcessed = DateTime.UtcNow;
  await ctx.SaveChangesAsync(token);
}
if (didAction) {
  user.LastProcessed = DateTime.UtcNow;
  await ctx.SaveChangesAsync(token);
}



this code is part of a method that gets called approximately 1000 times, additionally this code is in a loop that runs approximately 10 times. Meaning this code gets executed 10000 times in one round (approximately). A lot of this will be executed in parallel (all Tasks returned by this method are in a Task.WhenAll(...)).
Now I feel like CPU-Usage is too high sometimes and the issue is that sometimes the Database (postgres) even times out due to a lot of concurrency.
To solve timeouts I made a custom context (threadsafe context) that has locks on all operations (savechanges etc.) to prevent concurrency on Database level (even though postgres supports it but times out if I use too much concurrency, so I may lose performance here already). Right now all of these method calls use the same context, i am not creating a new context for each method call

I could also do the SaveChangesAsync after the Task.WhenAll, since the context is change tracking all the property changes (user.LastProcessed = ...), so a single save after all methods are done would work too. However I do not like this solution as it heavily relies on the fact that all methods DO exit at some point, if just one method call does never exit for whatever reason, then I lose data by not saving it.

How would you approach this?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

EF Core
C#CC# / help
2y ago
EF Core Loses Decimal Precision After Save
C#CC# / help
2y ago
✅ Update or create portfolio EF CORE
C#CC# / help
2y ago
Why Events Inserted Twice by EF Core?
C#CC# / help
3y ago