fix issue #I63AOC

This commit is contained in:
yubaolee 2022-11-26 22:01:35 +08:00
parent 8e25b6e8e3
commit eb77c29999
2 changed files with 25 additions and 0 deletions

View File

@ -36,6 +36,10 @@ namespace OpenAuth.Repository.Interface
/// </summary>
void ExecuteWithTransaction(Action action);
/// <summary>
/// ExecuteWithTransaction方法的异步方式
/// </summary>
Task ExecuteWithTransactionAsync(Func<Task> action);
/// <summary>
/// 返回DbContext,用于多线程等极端情况
/// </summary>
/// <returns></returns>

View File

@ -45,6 +45,27 @@ namespace OpenAuth.Repository
}
}
}
/// <summary>
/// ExecuteWithTransaction方法的异步方式
/// EF默认情况下每调用一次SaveChanges()都会执行一个单独的事务
/// 本接口实现在一个事务中可以多次执行SaveChanges()方法
/// </summary>
public async Task ExecuteWithTransactionAsync(Func<Task> action)
{
using (IDbContextTransaction transaction = _context.Database.BeginTransaction())
{
try
{
await action();
transaction.Commit();
}
catch (Exception ex)
{
transaction.Rollback();
throw ex;
}
}
}
/// <summary>
/// 返回DbContext,用于多线程等极端情况