首页 > 解决方案 > 如何解决“此 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 已完成;它不再可用。” 谁能指出我正确的方向?

标签: c#linq.net-coretransactionsentity-framework-core

解决方案


推荐阅读