1
0
mirror of https://github.com/bitwarden/server synced 2026-02-12 14:33:49 +00:00

Add explicit transaction in EF

This commit is contained in:
Thomas Rittson
2026-01-02 13:39:34 +10:00
parent 51800923ef
commit 0face3a06e

View File

@@ -809,6 +809,7 @@ public class CollectionRepository : Repository<Core.Entities.Collection, Collect
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);
await using var transaction = await dbContext.Database.BeginTransactionAsync();
try
{
@@ -819,9 +820,11 @@ public class CollectionRepository : Repository<Core.Entities.Collection, Collect
await dbContext.BulkCopyAsync(Mapper.Map<IEnumerable<CollectionUser>>(collectionUsers));
await dbContext.SaveChangesAsync();
await transaction.CommitAsync();
}
catch (Exception ex) when (DatabaseExceptionHelpers.IsDuplicateKeyException(ex))
{
await transaction.RollbackAsync();
throw new DuplicateDefaultCollectionException();
}
}