mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-04-05 08:37:28 +08:00
fix issue #I63AOC
This commit is contained in:
parent
8e25b6e8e3
commit
eb77c29999
@ -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>
|
||||
|
@ -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,用于多线程等极端情况
|
||||
|
Loading…
Reference in New Issue
Block a user