Created
February 18, 2020 04:48
-
-
Save zoint/20913cd4e34e18b01533b0c5e9efbed8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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