Skip to content

Commit

Permalink
fix: don't use user transaction, but use ef savechanges instead
Browse files Browse the repository at this point in the history
  • Loading branch information
jvandaal committed Jun 28, 2024
1 parent 834f549 commit dcfb344
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ public BackOfficeContext() { }
public BackOfficeContext(DbContextOptions<BackOfficeContext> options)
: base(options)
{
}
}

public DbSet<ParcelAddressRelation> ParcelAddressRelations { get; set; }

public async Task<ParcelAddressRelation> AddIdempotentParcelAddressRelation(
ParcelId parcelId,
AddressPersistentLocalId addressPersistentLocalId,
CancellationToken cancellationToken)
CancellationToken cancellationToken,
bool saveChanges = true)
{
var relation = await FindParcelAddressRelation(parcelId, addressPersistentLocalId, cancellationToken);
if (relation is not null)
Expand Down Expand Up @@ -62,7 +63,8 @@ public async Task<ParcelAddressRelation> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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<AddressWasRejectedBecauseOfReaddress>(async (commandHandler, message, ct) =>
Expand Down

0 comments on commit dcfb344

Please sign in to comment.