Dapper UPDATE not committing to Database before a SELECT is running

六月ゝ 毕业季﹏ 提交于 2019-12-25 07:49:12

问题


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!

来源:https://stackoverflow.com/questions/41370806/dapper-update-not-committing-to-database-before-a-select-is-running

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!