mirror of
https://github.com/meysamhadeli/booking-microservices.git
synced 2026-04-29 01:04:56 +08:00
refactor: Refactor handel DbUpdateConcurrencyException
This commit is contained in:
parent
f400fe511d
commit
916ba00215
@ -75,10 +75,36 @@ public abstract class AppDbContextBase : DbContext, IDbContext
|
|||||||
{
|
{
|
||||||
return base.SaveChangesAsync(cancellationToken);
|
return base.SaveChangesAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
|
//ref: https://learn.microsoft.com/en-us/ef/core/saving/concurrency?tabs=fluent-api#resolving-concurrency-conflicts
|
||||||
catch (DbUpdateConcurrencyException ex)
|
catch (DbUpdateConcurrencyException ex)
|
||||||
{
|
{
|
||||||
var data = ex.Entries.Single();
|
foreach (var entry in ex.Entries)
|
||||||
data.OriginalValues.SetValues(data.GetDatabaseValues() ?? throw new InvalidOperationException());
|
{
|
||||||
|
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);
|
return base.SaveChangesAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user