EFCode DBContextTransaction事务拦截是成功的,也能执行到回滚那一步,但是 事务回滚未生效?
调用事务的方法
[UnitOfWork]
public async Task CreateTableAndMenuAsync(string module, IList<Menu> menus)
{
await _moduleInstallService.AddAsync(new ModuleInstall { Code = module });
await _repository.CreateTableAndMenuAsync(module, menus);
}
事务拦截执行
public class UnitOfWorkAttribute : AbstractInterceptorAttribute
{
public override async Task Invoke(AspectContext context, AspectDelegate next)
{
var unitOfWork = context.ServiceProvider.GetService<IUnitOfWork>();
if (unitOfWork != null)
{
try
{
await unitOfWork.BeginTransactionAsync();
await next(context);
await unitOfWork.CommitTransactionAsync();
}
catch (Exception ex)
{
await unitOfWork.RollbackTransactionAsync();
throw;
}
}
else
{
await next(context);
}
}
}
事务实现
public async Task BeginTransactionAsync(CancellationToken cancellationToken = default)
{
await Database.BeginTransactionAsync(cancellationToken);
}
public async Task CommitTransactionAsync(CancellationToken cancellationToken = default)
{
await Database.CommitTransactionAsync(cancellationToken);
}
public async Task RollbackTransactionAsync(CancellationToken cancellationToken = default)
{
await Database.RollbackTransactionAsync(cancellationToken);
}