Для транзакции блок try-catch больше не нужен

This commit is contained in:
Оля Бизюкова 2024-12-09 14:45:33 +05:00
parent 4ccbe4361a
commit 42fe4dee50

View File

@ -101,34 +101,29 @@ public class ChangeLogRepository : IChangeLogRepository
var result = 0;
using var transaction = await db.Database.BeginTransactionAsync(token);
try
foreach (var dto in dtos)
{
foreach (var dto in dtos)
var updatedEntity = updatedEntities.GetValueOrDefault(dto.Id);
if (updatedEntity is null)
{
var updatedEntity = updatedEntities.GetValueOrDefault(dto.Id);
if (updatedEntity is null)
{
throw new ArgumentException($"Entity with id = {dto.Id} doesn't exist in Db", nameof(dto));
}
var newEntity = CreateEntityFromDto(idEditor, updatedEntity.IdDiscriminator, dto);
dbSet.Add(newEntity);
updatedEntity.IdNext = newEntity.Id;
updatedEntity.Obsolete = DateTimeOffset.UtcNow;
updatedEntity.IdEditor = idEditor;
throw new ArgumentException($"Entity with id = {dto.Id} doesn't exist in Db", nameof(dto));
}
result = await db.SaveChangesAsync(token);
await transaction.CommitAsync(token);
var newEntity = CreateEntityFromDto(idEditor, updatedEntity.IdDiscriminator, dto);
dbSet.Add(newEntity);
return result;
}
catch
{
await transaction.RollbackAsync(token);
throw;
updatedEntity.IdNext = newEntity.Id;
updatedEntity.Obsolete = DateTimeOffset.UtcNow;
updatedEntity.IdEditor = idEditor;
}
result = await db.SaveChangesAsync(token);
await transaction.CommitAsync(token);
return result;
}
public async Task<PaginationContainer<DataWithWellDepthAndSectionDto>> GetByDate(