mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-14 12:48:34 +08:00
Merge pull request #109 from meysamhadeli/refactor/refactor_db_update_concurrency_exception
refactor: Refactor handel DbUpdateConcurrencyException
This commit is contained in:
commit
9625878f9e
@ -75,10 +75,36 @@ public abstract class AppDbContextBase : DbContext, IDbContext
|
||||
{
|
||||
return base.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
//ref: https://learn.microsoft.com/en-us/ef/core/saving/concurrency?tabs=fluent-api#resolving-concurrency-conflicts
|
||||
catch (DbUpdateConcurrencyException ex)
|
||||
{
|
||||
var data = ex.Entries.Single();
|
||||
data.OriginalValues.SetValues(data.GetDatabaseValues() ?? throw new InvalidOperationException());
|
||||
foreach (var entry in ex.Entries)
|
||||
{
|
||||
var proposedValues = entry.CurrentValues;
|
||||
var databaseValues = entry.GetDatabaseValues();
|
||||
|
||||
if (databaseValues != null)
|
||||
{
|
||||
// update the original values with the database values
|
||||
entry.OriginalValues.SetValues(databaseValues);
|
||||
|
||||
// check for conflicts
|
||||
if (!proposedValues.Equals(databaseValues))
|
||||
{
|
||||
if (entry.Entity.GetType() == typeof(IAggregate))
|
||||
{
|
||||
// merge concurrency conflict for IAggregate
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new NotSupportedException(
|
||||
"Don't know how to handle concurrency conflicts for "
|
||||
+ entry.Metadata.Name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return base.SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user