Skip to content

Commit 0face3a

Browse files
committed
Add explicit transaction in EF
1 parent 5180092 commit 0face3a

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

src/Infrastructure.EntityFramework/Repositories/CollectionRepository.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,7 @@ public async Task CreateDefaultCollectionsAsync(Guid organizationId, IEnumerable
809809

810810
using var scope = ServiceScopeFactory.CreateScope();
811811
var dbContext = GetDatabaseContext(scope);
812+
await using var transaction = await dbContext.Database.BeginTransactionAsync();
812813

813814
try
814815
{
@@ -819,9 +820,11 @@ public async Task CreateDefaultCollectionsAsync(Guid organizationId, IEnumerable
819820
await dbContext.BulkCopyAsync(Mapper.Map<IEnumerable<CollectionUser>>(collectionUsers));
820821

821822
await dbContext.SaveChangesAsync();
823+
await transaction.CommitAsync();
822824
}
823825
catch (Exception ex) when (DatabaseExceptionHelpers.IsDuplicateKeyException(ex))
824826
{
827+
await transaction.RollbackAsync();
825828
throw new DuplicateDefaultCollectionException();
826829
}
827830
}

0 commit comments

Comments
 (0)