| | | 1 | | using ArturRios.Data.Relational.Core.Entities; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | |
| | | 4 | | namespace ArturRios.Data.Relational.Core.Configuration; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Base <see cref="DbContext" /> that applies shared conventions and refreshes the |
| | | 8 | | /// optimistic-concurrency stamp of modified <see cref="IVersionedEntity" /> instances on save. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <param name="options">The context options supplied by the configured provider.</param> |
| | 154 | 11 | | public abstract class BaseDbContext(DbContextOptions options) : DbContext(options) |
| | | 12 | | { |
| | | 13 | | // The parameterless SaveChanges / SaveChangesAsync overloads delegate to these two, so overriding |
| | | 14 | | // them covers every save entry point. |
| | | 15 | | |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public override int SaveChanges(bool acceptAllChangesOnSuccess) |
| | | 18 | | { |
| | 130 | 19 | | var bumped = BumpConcurrencyStamps(); |
| | | 20 | | |
| | | 21 | | try |
| | | 22 | | { |
| | 130 | 23 | | return base.SaveChanges(acceptAllChangesOnSuccess); |
| | | 24 | | } |
| | 20 | 25 | | catch |
| | | 26 | | { |
| | 20 | 27 | | RestoreConcurrencyStamps(bumped); |
| | 20 | 28 | | throw; |
| | | 29 | | } |
| | 110 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public override async Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, |
| | | 34 | | CancellationToken cancellationToken = default) |
| | | 35 | | { |
| | 36 | 36 | | var bumped = BumpConcurrencyStamps(); |
| | | 37 | | |
| | | 38 | | try |
| | | 39 | | { |
| | 36 | 40 | | return await base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken).ConfigureAwait(false); |
| | | 41 | | } |
| | 2 | 42 | | catch |
| | | 43 | | { |
| | 2 | 44 | | RestoreConcurrencyStamps(bumped); |
| | 2 | 45 | | throw; |
| | | 46 | | } |
| | 34 | 47 | | } |
| | | 48 | | |
| | | 49 | | private List<(IVersionedEntity Entity, Guid Stamp)> BumpConcurrencyStamps() |
| | | 50 | | { |
| | 166 | 51 | | var bumped = new List<(IVersionedEntity Entity, Guid Stamp)>(); |
| | | 52 | | |
| | 384 | 53 | | foreach (var entry in ChangeTracker.Entries<IVersionedEntity>() |
| | 218 | 54 | | .Where(e => e.State == EntityState.Modified)) |
| | | 55 | | { |
| | 26 | 56 | | bumped.Add((entry.Entity, entry.Entity.ConcurrencyStamp)); |
| | 26 | 57 | | entry.Entity.ConcurrencyStamp = Guid.NewGuid(); |
| | | 58 | | } |
| | | 59 | | |
| | 166 | 60 | | return bumped; |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | // The save never landed, so the in-memory bump must not survive: keeping it would leave the |
| | | 64 | | // entity carrying a stamp the database never stored, and every retry would then fail as a |
| | | 65 | | // concurrency conflict. |
| | | 66 | | private static void RestoreConcurrencyStamps(List<(IVersionedEntity Entity, Guid Stamp)> bumped) |
| | | 67 | | { |
| | 64 | 68 | | foreach (var (entity, stamp) in bumped) |
| | | 69 | | { |
| | 10 | 70 | | entity.ConcurrencyStamp = stamp; |
| | | 71 | | } |
| | 22 | 72 | | } |
| | | 73 | | } |