| | | 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="VersionedEntity" /> instances on save. |
| | | 9 | | /// </summary> |
| | | 10 | | /// <param name="options">The context options supplied by the configured provider.</param> |
| | 37 | 11 | | public abstract class BaseDbContext(DbContextOptions options) : DbContext(options) |
| | | 12 | | { |
| | | 13 | | /// <inheritdoc /> |
| | | 14 | | public override int SaveChanges() |
| | 27 | 15 | | { |
| | 27 | 16 | | BumpConcurrencyStamps(); |
| | | 17 | | |
| | 27 | 18 | | return base.SaveChanges(); |
| | 26 | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <inheritdoc /> |
| | | 22 | | public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default) |
| | 10 | 23 | | { |
| | 10 | 24 | | BumpConcurrencyStamps(); |
| | | 25 | | |
| | 10 | 26 | | return base.SaveChangesAsync(cancellationToken); |
| | 10 | 27 | | } |
| | | 28 | | |
| | | 29 | | private void BumpConcurrencyStamps() |
| | 37 | 30 | | { |
| | 117 | 31 | | foreach (var entry in ChangeTracker.Entries<VersionedEntity>() |
| | 44 | 32 | | .Where(e => e.State == EntityState.Modified)) |
| | 3 | 33 | | { |
| | 3 | 34 | | entry.Entity.ConcurrencyStamp = Guid.NewGuid(); |
| | 3 | 35 | | } |
| | 37 | 36 | | } |
| | | 37 | | } |