From eb77c2999994e2071da5f5160cfff70bc08f9b4f Mon Sep 17 00:00:00 2001 From: yubaolee Date: Sat, 26 Nov 2022 22:01:35 +0800 Subject: [PATCH] fix issue #I63AOC --- OpenAuth.Repository/Interface/IUnitWork.cs | 4 ++++ OpenAuth.Repository/UnitWork.cs | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/OpenAuth.Repository/Interface/IUnitWork.cs b/OpenAuth.Repository/Interface/IUnitWork.cs index 57ad2817..03133822 100644 --- a/OpenAuth.Repository/Interface/IUnitWork.cs +++ b/OpenAuth.Repository/Interface/IUnitWork.cs @@ -36,6 +36,10 @@ namespace OpenAuth.Repository.Interface /// void ExecuteWithTransaction(Action action); /// + /// ExecuteWithTransaction方法的异步方式 + /// + Task ExecuteWithTransactionAsync(Func action); + /// /// 返回DbContext,用于多线程等极端情况 /// /// diff --git a/OpenAuth.Repository/UnitWork.cs b/OpenAuth.Repository/UnitWork.cs index f065740c..c5c348fd 100644 --- a/OpenAuth.Repository/UnitWork.cs +++ b/OpenAuth.Repository/UnitWork.cs @@ -45,6 +45,27 @@ namespace OpenAuth.Repository } } } + /// + /// ExecuteWithTransaction方法的异步方式 + /// EF默认情况下,每调用一次SaveChanges()都会执行一个单独的事务 + /// 本接口实现在一个事务中可以多次执行SaveChanges()方法 + /// + public async Task ExecuteWithTransactionAsync(Func action) + { + using (IDbContextTransaction transaction = _context.Database.BeginTransaction()) + { + try + { + await action(); + transaction.Commit(); + } + catch (Exception ex) + { + transaction.Rollback(); + throw ex; + } + } + } /// /// 返回DbContext,用于多线程等极端情况