Skip to content

Instantly share code, notes, and snippets.

@zoint
Created February 18, 2020 04:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zoint/20913cd4e34e18b01533b0c5e9efbed8 to your computer and use it in GitHub Desktop.
Save zoint/20913cd4e34e18b01533b0c5e9efbed8 to your computer and use it in GitHub Desktop.
private async Task SaveChangesAsync()
{
try
{
await _dbContext.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException ex)
{
//adapted from: https://docs.microsoft.com/en-us/ef/core/saving/concurrency
foreach (var entry in ex.Entries)
{
if (entry.Entity is Shift)
{
var proposedValues = entry.CurrentValues;
var databaseValues = entry.GetDatabaseValues();
foreach (var property in proposedValues.Properties)
{
var proposedValue = proposedValues[property];
var databaseValue = databaseValues[property];
// decide which value should be written to database
// in the future we may need to update this if a
proposedValues[property] = proposedValue;
}
// Refresh original values to bypass next concurrency check
entry.OriginalValues.SetValues(databaseValues);
await SaveChangesAsync();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment