forked from ddrilling/AsbCloudServer
Сохранение данных с возможностью отката транзации в методе Insert в ChangeLogRepositoryAbstract
This commit is contained in:
parent
770a370d7f
commit
d6b4f90f4c
@ -117,6 +117,22 @@ public abstract class ChangeLogRepositoryAbstract<TEntity, TDto, TRequest> : ICh
|
|||||||
}
|
}
|
||||||
|
|
||||||
public async Task<int> InsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
|
public async Task<int> InsertRange(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
|
||||||
|
{
|
||||||
|
using var transaction = db.Database.BeginTransaction();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = await InsertRangeWithoutTransaction(idUser, dtos, token);
|
||||||
|
await transaction.CommitAsync(token);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
await transaction.RollbackAsync(token);
|
||||||
|
throw;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task<int> InsertRangeWithoutTransaction(int idUser, IEnumerable<TDto> dtos, CancellationToken token)
|
||||||
{
|
{
|
||||||
var result = 0;
|
var result = 0;
|
||||||
if (dtos.Any())
|
if (dtos.Any())
|
||||||
@ -138,6 +154,7 @@ public abstract class ChangeLogRepositoryAbstract<TEntity, TDto, TRequest> : ICh
|
|||||||
|
|
||||||
result += await SaveChangesWithExceptionHandling(token);
|
result += await SaveChangesWithExceptionHandling(token);
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -248,7 +265,7 @@ public abstract class ChangeLogRepositoryAbstract<TEntity, TDto, TRequest> : ICh
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
result += await Clear(idUser, request, token);
|
result += await Clear(idUser, request, token);
|
||||||
result += await InsertRange(idUser, dtos, token);
|
result += await InsertRangeWithoutTransaction(idUser, dtos, token);
|
||||||
|
|
||||||
await transaction.CommitAsync(token);
|
await transaction.CommitAsync(token);
|
||||||
return result;
|
return result;
|
||||||
|
Loading…
Reference in New Issue
Block a user