Closed gmf520 closed 2 years ago
当一次请求当中,执行IUnitOfWork.Commit()提交事务后,(MySql中)无法进行数据查询,无法在进行数据CUD之后再次提交事务,这与原生EFCore的表现不符
IUnitOfWork.Commit()
例如,执行如下Action,在执行await manager.CreateAsync(new Role() { Name = "测试角色4" + token, Remark = "测试角色002描述" });时,MySql中会报异常The transaction associated with this command is not the connection’s active transaction
await manager.CreateAsync(new Role() { Name = "测试角色4" + token, Remark = "测试角色002描述" });
The transaction associated with this command is not the connection’s active transaction
[HttpPost] [Description("测试2")] public async Task<int> Test02() { IServiceProvider provider = HttpContext.RequestServices; string token = Guid.NewGuid().ToString("N").Substring(0, 5); RoleManager<Role> manager = provider.GetRequiredService<RoleManager<Role>>(); await manager.CreateAsync(new Role() { Name = "测试角色3" + token, Remark = "测试角色描述" }); await manager.CreateAsync(new Role() { Name = "测试角色4" + token, Remark = "测试角色描述" }); return provider.GetRequiredService<RoleManager<Role>>().Roles.Count(); }
提交事务后,数据上下文仍带着已经释放的事务对象,导致后续的操作无法正常执行
BeginOrUseTransaction
DbTransaction
transaction.Connection == null
DbContext
context.Database.UseTransaction(null)
描述一下BUG
当一次请求当中,执行
IUnitOfWork.Commit()
提交事务后,(MySql中)无法进行数据查询,无法在进行数据CUD之后再次提交事务,这与原生EFCore的表现不符重现步骤
例如,执行如下Action,在执行
await manager.CreateAsync(new Role() { Name = "测试角色4" + token, Remark = "测试角色002描述" });
时,MySql中会报异常The transaction associated with this command is not the connection’s active transaction
原因分析
提交事务后,数据上下文仍带着已经释放的事务对象,导致后续的操作无法正常执行
解决方案
BeginOrUseTransaction
时检查事务对象DbTransaction
的状态,如果transaction.Connection == null
则重新创建事务DbContext
释放事务:context.Database.UseTransaction(null)