/// <summary> /// 生成测试数据 /// </summary> /// <param name="id"></param> /// <returns></returns> private async Task <CustomerFinance> InsertCusFinance(long id) { var cusFinance = new CustomerFinance { Account = "alpha2008", Id = id, Balance = 0 }; await _cusFinanceRsp.InsertAsync(cusFinance); return(cusFinance); }
public async Task TestUOWRollback() { var id = IdGenerater.GetNextId(); var id2 = IdGenerater.GetNextId(); var account = "alpha008"; var customer = new Customer() { Id = id, Account = account, Nickname = "招财猫", Realname = "张发财" }; var customer2 = new Customer() { Id = id2, Account = account, Nickname = "招财猫02", Realname = "张发财02" }; var cusFinance = new CustomerFinance { Account = account, Id = id, Balance = 0 }; var newNickName = "招财猫008"; var newRealName = "张发财008"; var newBalance = 100m; try { _unitOfWork.BeginTransaction(); // insert efcore await _customerRsp.InsertAsync(customer); await _customerRsp.InsertAsync(customer2); await _cusFinanceRsp.InsertAsync(cusFinance); //update single customer.Nickname = newNickName; await _customerRsp.UpdateAsync(customer, UpdatingProps <Customer>(c => c.Nickname)); cusFinance.Balance = newBalance; await _cusFinanceRsp.UpdateAsync(cusFinance, UpdatingProps <CustomerFinance>(c => c.Balance)); //update batchs await _customerRsp.UpdateRangeAsync(x => x.Id == id, c => new Customer { Realname = newRealName }); //delete raw sql await _customerRsp.DeleteAsync(id2); throw new Exception(); _unitOfWork.Commit(); } catch (Exception ex) { _unitOfWork.Rollback(); } finally { _unitOfWork.Dispose(); } var cusTotal = await _customerRsp.CountAsync(x => x.Id == id || x.Id == id2); Assert.Equal(0, cusTotal); var customerFromDb = await _customerRsp.FindAsync(id); var financeFromDb = await _customerRsp.FetchAsync(c => c, x => x.Id == id); Assert.Null(customerFromDb); Assert.Null(financeFromDb); }