Dapper UPDATE not committing to Database before a

2019-09-07 11:34发布

问题:

I ran into an interesting issue today that I've never seen before. I am using Dapper v1.42.0 inside my MVC app to update records in a SQL database. After the update runs, I am redirecting to an action that selects the same data that was just updated. The issue is, that the SELECT statement is not getting the updated record. It seems like the database hasn't committed the UPDATE before the SELECT is run, but I don't see how that is possible. Here is the code that is called from a client-side ajax call to UPDATE:

 [HttpPost]
    public ActionResult UpdateCommission(CommissionDetailVM updatedCommModel)
    {
        using (SqlConnection sqlConn = new SqlConnection(connectionString))
        {
            string queryTable;
            if (updatedCommModel.DocType == "Invoice")
            {
                queryTable = "[MSI$Sales Invoice Line]";
            }
            else
            {
                queryTable = "[MSI$Sales Cr_Memo Line]";
            }
            sqlConn.Execute(String.Format("UPDATE {0} SET FreightCharge = @freight, PalletCharge = @pallet, [Territory Code] = @territory, BrokerCommission = @broker,  MiscCharge = @misc WHERE [Document No_] = @docNo AND [Line No_] = @lineNo", queryTable),
                 new
                 {
                     @docNo = updatedCommModel.InvoiceNo,
                     @lineNo = updatedCommModel.LineNum,
                     @freight = updatedCommModel.Freight,
                     @territory = updatedCommModel.Broker,
                     @broker = updatedCommModel.Commission,
                     @misc = updatedCommModel.MiscCharge,
                     @pallet = updatedCommModel.Pallets
                 });

        }
        CommissionUpdate model = new CommissionUpdate();
        model.DocumentNumber = updatedCommModel.InvoiceNo;
        model.DocumentType = updatedCommModel.DocType;
        return RedirectToAction("Commission", new { commModel = model });
    }

And here is the action (SELECT) that is being redirected to after the UPDATE:

[HttpPost]
    public ActionResult Commission(CommissionUpdate commModel)
    {
        string queryTable;
        if (commModel.DocumentType == "Invoice")
        {
            queryTable = "[MSI$Sales Invoice Line]";
        }
        else
        {
            queryTable = "[MSI$Sales Cr_Memo Line]";
        }

        using (SqlConnection sqlConn = new SqlConnection(connectionString))
        {
            var result = sqlConn.Query<CommissionDetail>(String.Format("SELECT [Document No_] as invoiceNo, [Line No_] as lineNum, Description, FreightCharge as freight, PalletCharge as pallets, MiscCharge, [Territory Code] as broker, BrokerCommission as commission FROM {0} WHERE [Document No_] = @docNo Order by [Line No_]", queryTable),
                new { @docNo = commModel.DocumentNumber }).ToList();
            ViewBag.Brokers = sqlConn.Query<KeyValue>("SELECT LTRIM(RTRIM(Code)) as [Key], LTRIM(RTRIM(Name)) as [Value] FROM [MSI$Territory]").ToList();
            commModel.CommissionDetails = result;

        }

        return View("Commission", commModel);
    }

Any help/ideas would be much appreciated! Cheers!