diff --git a/src/ParcelRegistry.Api.BackOffice.Abstractions/BackOfficeContext.cs b/src/ParcelRegistry.Api.BackOffice.Abstractions/BackOfficeContext.cs index 945c8e64..4432298b 100644 --- a/src/ParcelRegistry.Api.BackOffice.Abstractions/BackOfficeContext.cs +++ b/src/ParcelRegistry.Api.BackOffice.Abstractions/BackOfficeContext.cs @@ -18,14 +18,15 @@ public BackOfficeContext() { } public BackOfficeContext(DbContextOptions options) : base(options) { - } + } public DbSet ParcelAddressRelations { get; set; } public async Task AddIdempotentParcelAddressRelation( ParcelId parcelId, AddressPersistentLocalId addressPersistentLocalId, - CancellationToken cancellationToken) + CancellationToken cancellationToken, + bool saveChanges = true) { var relation = await FindParcelAddressRelation(parcelId, addressPersistentLocalId, cancellationToken); if (relation is not null) @@ -62,7 +63,8 @@ public async Task AddIdempotentParcelAddressRelation( public async Task RemoveIdempotentParcelAddressRelation( ParcelId parcelId, AddressPersistentLocalId addressPersistentLocalId, - CancellationToken cancellationToken) + CancellationToken cancellationToken, + bool saveChanges = true) { var relation = await FindParcelAddressRelation(parcelId, addressPersistentLocalId, cancellationToken); if (relation is not null) diff --git a/src/ParcelRegistry.Consumer.Address/Projections/CommandHandlingKafkaProjection.cs b/src/ParcelRegistry.Consumer.Address/Projections/CommandHandlingKafkaProjection.cs index ca281b3e..4f239b3b 100644 --- a/src/ParcelRegistry.Consumer.Address/Projections/CommandHandlingKafkaProjection.cs +++ b/src/ParcelRegistry.Consumer.Address/Projections/CommandHandlingKafkaProjection.cs @@ -10,7 +10,6 @@ namespace ParcelRegistry.Consumer.Address.Projections using Be.Vlaanderen.Basisregisters.GrAr.Provenance; using Be.Vlaanderen.Basisregisters.ProjectionHandling.Connector; using Microsoft.EntityFrameworkCore; - using NodaTime; using NodaTime.Text; using Parcel; using Parcel.Commands; @@ -210,8 +209,6 @@ await DetachBecauseRemoved( } } - await backOfficeContext.Database.BeginTransactionAsync(ct); - foreach (var parcelId in commandByParcels.Select(x => x.ParcelId)) { var parcel = await parcels.GetAsync(new ParcelStreamId(parcelId), ct); @@ -229,16 +226,16 @@ await DetachBecauseRemoved( foreach (var addressPersistentLocalId in addressesToRemove) { - await backOfficeContext.RemoveIdempotentParcelAddressRelation(parcelId, addressPersistentLocalId, ct); + await backOfficeContext.RemoveIdempotentParcelAddressRelation(parcelId, addressPersistentLocalId, ct, saveChanges: false); } foreach (var addressPersistentLocalId in addressesToAdd) { - await backOfficeContext.AddIdempotentParcelAddressRelation(parcelId, addressPersistentLocalId, ct); + await backOfficeContext.AddIdempotentParcelAddressRelation(parcelId, addressPersistentLocalId, ct, saveChanges: false); } } - await backOfficeContext.Database.CommitTransactionAsync(ct); + await backOfficeContext.SaveChangesAsync(ct); }); When(async (commandHandler, message, ct) =>