我首先拥有一个数据库,.net core 2.1 Entity Framework剃刀页面应用程序。创建银行业务应用程序是一个家庭作业问题,但是我不知道为什么这小部分不起作用。当我将代码分解为单独的方法时,我的交易无法完成,甚至无法正确回滚。我得到的错误是“此SqlTransaction已完成;它不再可用。”当我将代码从单独的方法中拉出并放入“ TransferCheckingToSaving”时,它可以正常工作。
我尝试将事务变量作为输入传递给每个子方法,并且这些方法不会出错。当提交到“ TransferCheckingToSaving” int时,将引发错误。
django.jQuery('select.selectfilter, select.selectfilterstacked').each(function () {
var $el = $(this),
data = $el.data();
SelectFilter.init($el.attr('id'), data.fieldName, parseInt(data.isStacked, 10));
});
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;
}
}
我希望事务成功完成,但是会引发错误“此SqlTransaction已完成;它不再可用。”谁能指出我正确的方向?