Фикс ChangeLogRepository

This commit is contained in:
Степанов Дмитрий 2024-02-16 12:00:46 +03:00
parent e2764504aa
commit 8550342879

View File

@ -30,7 +30,6 @@ public abstract class ChangeLogRepositoryAbstract<TDto, TEntity, TRequest> : ICh
var result = 0; var result = 0;
if (dtos.Any()) if (dtos.Any())
{ {
using var transaction = await db.Database.BeginTransactionAsync(token);
var entities = dtos.Select(Convert); var entities = dtos.Select(Convert);
var creation = DateTimeOffset.UtcNow; var creation = DateTimeOffset.UtcNow;
var dbSet = db.Set<TEntity>(); var dbSet = db.Set<TEntity>();
@ -47,7 +46,6 @@ public abstract class ChangeLogRepositoryAbstract<TDto, TEntity, TRequest> : ICh
} }
result += await SaveChangesWithExceptionHandling(token); result += await SaveChangesWithExceptionHandling(token);
await transaction.CommitAsync(token);
} }
return result; return result;
} }
@ -104,8 +102,17 @@ public abstract class ChangeLogRepositoryAbstract<TDto, TEntity, TRequest> : ICh
} }
result += await SaveChangesWithExceptionHandling(token); result += await SaveChangesWithExceptionHandling(token);
await transaction.CommitAsync(token);
return result; try
{
await transaction.CommitAsync(token);
return result;
}
catch
{
await transaction.RollbackAsync(token);
throw;
}
} }
public async Task<int> UpdateOrInsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token) public async Task<int> UpdateOrInsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
@ -148,8 +155,16 @@ public abstract class ChangeLogRepositoryAbstract<TDto, TEntity, TRequest> : ICh
using var transaction = await db.Database.BeginTransactionAsync(token); using var transaction = await db.Database.BeginTransactionAsync(token);
result += await Clear(idUser, request, token); result += await Clear(idUser, request, token);
result += await InsertRange(idUser, dtos, token); result += await InsertRange(idUser, dtos, token);
await transaction.CommitAsync(token); try
return result; {
await transaction.CommitAsync(token);
return result;
}
catch
{
await transaction.RollbackAsync(token);
throw;
}
} }
public async Task<int> DeleteRange(int idUser, IEnumerable<int> ids, CancellationToken token) public async Task<int> DeleteRange(int idUser, IEnumerable<int> ids, CancellationToken token)