| | | 1 | | using System.Reflection; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | using Microsoft.EntityFrameworkCore.ChangeTracking; |
| | | 4 | | using MechanicsSoftware.Application.Abstractions; |
| | | 5 | | using MechanicsSoftware.Domain.Entities; |
| | | 6 | | using MechanicsSoftware.Domain.ValueObjects; |
| | | 7 | | using MechanicsSoftware.Domain.Enums; |
| | | 8 | | using MechanicsSoftware.Domain.Exceptions; |
| | | 9 | | |
| | | 10 | | namespace MechanicsSoftware.Infrastructure.Persistence; |
| | | 11 | | |
| | | 12 | | public sealed class AppDbContext : DbContext, IAppDbContext |
| | | 13 | | { |
| | 510 | 14 | | public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { } |
| | 66 | 15 | | |
| | | 16 | | public DbSet<User> Users { get; set; } = null!; |
| | | 17 | | public DbSet<Customer> Customers { get; set; } = null!; |
| | | 18 | | public DbSet<Vehicle> Vehicles { get; set; } = null!; |
| | | 19 | | public DbSet<Part> Parts { get; set; } = null!; |
| | | 20 | | public DbSet<Service> Services { get; set; } = null!; |
| | | 21 | | public DbSet<ServiceOrder> ServiceOrders { get; set; } = null!; |
| | | 22 | | |
| | | 23 | | protected override void OnModelCreating(ModelBuilder modelBuilder) |
| | 2 | 24 | | { |
| | 3 | 25 | | modelBuilder.ApplyConfigurationsFromAssembly(Assembly.GetExecutingAssembly()); |
| | 3 | 26 | | } |
| | 1 | 27 | | |
| | | 28 | | // Workaround for EF Core 9.0.14 + Npgsql 9.0.4: brand-new owned entities (collection items |
| | | 29 | | // and owned scalars) are sometimes attached as Modified instead of Added even when the |
| | | 30 | | // owner is loaded with .Include(...) of the navigation. The resulting UPDATE affects 0 |
| | | 31 | | // rows and throws DbUpdateConcurrencyException. We promote those entries before saving. |
| | | 32 | | public override async Task<int> SaveChangesAsync(CancellationToken cancellationToken = default) |
| | 170 | 33 | | { |
| | 170 | 34 | | ChangeTracker.DetectChanges(); |
| | 170 | 35 | | PromoteNewOwnedCollectionItems(); |
| | 170 | 36 | | await PromoteNewOwnedScalarsAsync(cancellationToken); |
| | | 37 | | |
| | 170 | 38 | | var auto = ChangeTracker.AutoDetectChangesEnabled; |
| | 170 | 39 | | ChangeTracker.AutoDetectChangesEnabled = false; |
| | | 40 | | try |
| | 170 | 41 | | { |
| | 170 | 42 | | return await base.SaveChangesAsync(cancellationToken); |
| | | 43 | | } |
| | | 44 | | finally |
| | 170 | 45 | | { |
| | 170 | 46 | | ChangeTracker.AutoDetectChangesEnabled = auto; |
| | 170 | 47 | | } |
| | 170 | 48 | | } |
| | | 49 | | |
| | | 50 | | // Owned-collection items in this domain are append-only (ServiceItems, PartItems, |
| | | 51 | | // StockMovements). Any Modified entry is therefore a freshly-added item. No DB round-trip. |
| | | 52 | | private void PromoteNewOwnedCollectionItems() |
| | 170 | 53 | | { |
| | 1334 | 54 | | foreach (var entry in ChangeTracker.Entries()) |
| | 412 | 55 | | { |
| | 782 | 56 | | if (entry.State != EntityState.Modified) continue; |
| | 42 | 57 | | var ownership = entry.Metadata.FindOwnership(); |
| | 84 | 58 | | if (ownership is null || ownership.IsUnique) continue; |
| | 0 | 59 | | entry.State = EntityState.Added; |
| | 0 | 60 | | } |
| | 170 | 61 | | } |
| | | 62 | | |
| | | 63 | | // Owned scalars (e.g. Budget) ARE mutated after creation (Approve/Reject), so we cannot |
| | | 64 | | // blindly flip Modified → Added. A single DB lookup distinguishes new from existing. |
| | | 65 | | private async Task PromoteNewOwnedScalarsAsync(CancellationToken cancellationToken) |
| | 170 | 66 | | { |
| | 170 | 67 | | var candidates = ChangeTracker.Entries() |
| | 170 | 68 | | .Where(e => e.State == EntityState.Modified |
| | 170 | 69 | | && e.Metadata.FindOwnership() is { IsUnique: true }) |
| | 170 | 70 | | .ToList(); |
| | | 71 | | |
| | 534 | 72 | | foreach (var entry in candidates) |
| | 12 | 73 | | await PromoteIfMissingAsync(entry, cancellationToken); |
| | 170 | 74 | | } |
| | | 75 | | |
| | | 76 | | private static async Task PromoteIfMissingAsync(EntityEntry entry, CancellationToken ct) |
| | 12 | 77 | | { |
| | 12 | 78 | | if (await entry.GetDatabaseValuesAsync(ct) is null) |
| | 0 | 79 | | entry.State = EntityState.Added; |
| | 12 | 80 | | } |
| | | 81 | | } |