using MediatApi.Entity;using MediatR;using Microsoft.EntityFrameworkCore;using Microsoft.EntityFrameworkCore.Storage;namespace MediatApi.Helper{ public class WyyDbContext: testContext { private IDbContextTransaction _currentTransaction; public IDbContextTransaction GetCurrentTransaction() => _currentTransaction; public bool HasActiveTransaction => _currentTransaction != null; public async Task CommitTransactionAsync(IDbContextTransaction transaction) { if (transaction == null) throw new ArgumentNullException(nameof(transaction)); if (transaction != _currentTransaction) throw new InvalidOperationException($"Transaction {transaction.TransactionId} is not current"); try { await SaveChangesAsync(); transaction.Commit(); } catch { RollbackTransaction(); throw; } finally { if (_currentTransaction != null) { _currentTransaction.Dispose(); _currentTransaction = null; } } } public void RollbackTransaction() { try { _currentTransaction?.Rollback(); } finally { if (_currentTransaction != null) { _currentTransaction.Dispose(); _currentTransaction = null; } } } }}
经验总结扩展阅读
- 常用Python库整理
- 2023年1月24日建造寺庙好吗 2023年1月24日建造寺庙行吗
- 中式装修风格怎么搭配更好看 中式风格装修注意事项有哪些
- 2023年1月24日修筑堤坝好吗 2023年1月24日是修筑堤坝的黄道吉日吗
- 2023年1月24日粉刷墙壁好吗 2023年1月24日是粉刷墙壁的黄道吉日吗
- 2023年1月24日厨房移位好吗 2023年1月24日厨房移位行吗
- 加拿大高中排名 加拿大高中排名
- 2023中秋祝福语送客户
- 2023中秋祝福语送领导
- 2023送给领导的中秋祝福语