c# - 如何解决“此 SQLTransaction 已完成;它不再可用?(linq/EF)
问题描述
我首先有一个数据库,.net core 2.1 Entity Framework razor 页面应用程序。创建一个银行应用程序是一个家庭作业问题,但我不知道为什么这个小部分不起作用。当我将代码分解为单独的方法时,我的事务无法完成甚至无法正确回滚。我得到的错误是“此 SqlTransaction 已完成;它不再可用。” 当我将代码从单独的方法中拉出并进入“TransferCheckingToSaving”时,它可以正常工作。
我试图将事务变量作为输入传递给每个子方法,并且这些方法不会出错。当它进入“TransferCheckingToSaving”的提交时,它会抛出错误。
public bool TransferCheckingToSaving(long checkingAccountNum, long savingAccountNum, decimal amount, decimal transactionFee)
{
bool ret = false;
using (var contextTransaction = _context.Database.BeginTransaction())
{
try
{
bool updateSavingSuccess = UpdateSavingBalanceTR(savingAccountNum, amount, contextTransaction);
if (updateSavingSuccess != true)
throw new Exception("The saving account balance did not update successfully while transferring from checking to saving");
bool updateCheckingSuccess = UpdateCheckingBalanceTR(checkingAccountNum, -1 * amount, contextTransaction);
if (updateCheckingSuccess != true)
throw new Exception("The checking account balance did not update successfully while transferring from checking to saving");
bool addToTransHistSuccess = AddToTransactionHistory(checkingAccountNum, savingAccountNum, amount, 100, 0, contextTransaction);
if (addToTransHistSuccess != true)
throw new Exception("Failed to update the transaction history while transferring from checking to saving");
else
{
_context.SaveChanges();
contextTransaction.Commit();
ret = true;
CacheAbstraction cabs = new CacheAbstraction();
cabs.Remove(CacheNameFacade.TRCACHENAME + ":" + checkingAccountNum);
}
}
catch(Exception ex)
{
contextTransaction.Rollback();
throw ex;
}
}
return ret;
}
private bool UpdateCheckingBalanceTR(long checkingAccountNum, decimal amount, IDbContextTransaction transaction)
{
bool ret = false;
try
{
using (transaction)
{
CheckingAccounts checkingAcct = (from c in _context.CheckingAccounts where c.CheckingAccountNumber == checkingAccountNum select c).FirstOrDefault<CheckingAccounts>();
checkingAcct.Balance += (decimal)amount;
if (checkingAcct.Balance < 0)
throw new Exception("The checking balance of " + (checkingAcct.Balance -= amount).ToString() + " is too low to complete this transaction");
_context.SaveChanges();
ret = true;
return ret;
}
}
catch (Exception)
{
throw;
}
//return ret;
}
private bool UpdateSavingBalanceTR(long savingAccountNum, decimal amount, IDbContextTransaction transaction)
{
bool ret = false;
try
{
using (transaction)
{
SavingAccounts savingAcct = (from s in _context.SavingAccounts where s.SavingAccountNumber == savingAccountNum select s).FirstOrDefault<SavingAccounts>();
savingAcct.Balance += (decimal)amount;
if (savingAcct.Balance < 0)
throw new Exception("The checking balance of " + (savingAcct.Balance -= amount).ToString() + " is too low to complete this transaction");
_context.SaveChanges();
ret = true;
return ret;
}
}
catch (Exception)
{
throw;
}
}
private bool AddToTransactionHistory(long checkingAccountNum, long savingAccountNum, decimal amount, int transTypeId, decimal transFee, IDbContextTransaction transaction)
{
bool ret = false;
try
{
using (transaction)
{
TransactionHistories transHist = new TransactionHistories
{
CheckingAccountNumber = checkingAccountNum,
SavingAccountNumber = savingAccountNum,
Amount = amount,
TransactionFee = transFee,
TransactionTypeId = (long)transTypeId,
};
_context.TransactionHistories.Add(transHist);
CacheAbstraction cabs = new CacheAbstraction();
cabs.Remove(CacheNameFacade.TRCACHENAME + ":" + checkingAccountNum); //the checking account is the same for each unique user
_context.SaveChanges();
ret = true;
return ret;
}
}
catch (Exception)
{
throw;
}
}
我希望事务能够成功完成,但它会抛出错误“此 SqlTransaction 已完成;它不再可用。” 谁能指出我正确的方向?
解决方案
推荐阅读
- svg - 如何将 SVG 组元素转换到根 SVG 的中心?
- jquery - 从外部文件jQuery加载表数据
- html - 工具提示物化不会出现在每个元素的顶部
- javascript - AMcharts 图表 y 轴上的 2 个时间序列
- wordpress - 用于取消已删除产品的 WC 订单的 Wordpress cronjob?
- graphql - 返回 graphql 错误时,如何捕获 RelayObervable Unhandled 错误?
- c# - 如何在 WPF 中覆盖最大化按钮?
- c++ - 尝试设置唯一指针布尔数组的值时出现 BAD_ACCESS 错误
- python - 如何识别使用 pydal 使用 sqlalchemy 创建的 sqlite 数据库?
- android - 插页式广告关闭后如何在 recyclerview 适配器中执行事件意图?