Linq to sql更新现在可以工作了

本文关键字:工作 to sql 更新 Linq | 更新日期: 2023-09-27 18:06:03

我正在尝试使用linq从数据库更新一些数据。我在以下方法中编写的所有其他代码都如我预期的那样工作,但在标记行处的更新代码不起作用。数据库中没有更改。我已经调试了它,我得到的值,我期望从用户。唯一的问题是我期望在数据库中的变化。有什么问题吗?

    private void addToCart()
    {
        FixbayDBDataContext db = new FixbayDBDataContext();
        if (Session["CustomerAuthentication"] != null)
        {
            CartTbl cartTbl = new CartTbl();
            CartShoeTbl csTbl = new CartShoeTbl();
            int cusID = Convert.ToInt32(Session["CustomerAuthentication"]);
            int shoeID = Convert.ToInt32(sizeDdl.Text);

            var idQuery = from c in db.CartTbls.AsQueryable()
                          where c.CustomerID == cusID
                          select new{c.CartID,c.CustomerID};

            var shoeQuery = from s in db.ShoeTbls.AsQueryable()
                            join m in db.ShoeModelTbls on s.ModelID equals m.ModelID
                            where s.ShoeID == shoeID
                            select new { s.ShoeID, m.Price };
            int quantity = Convert.ToInt32(quantityDdl.Text);
            double shoePrice = (double)shoeQuery.First().Price;
            double totalPrice = quantity * shoePrice;
            //int shoeID = (int)shoeQuery.First().ShoeID;
            //int lastShoeID = 0;
            if (idQuery.Any())
            {
                int cartID = (int)idQuery.FirstOrDefault().CartID;
                var cartShoeQuery = from ca in db.CartShoeTbls.AsQueryable()
                                    where ca.CartID == cartID && ca.ShoeID == shoeID
                                    select ca;
                if (cartShoeQuery.Any())
                {
                    csTbl.Quantity += Convert.ToInt32(quantityDdl.Text);
                    db.SubmitChanges();
                }
                else
                {
                    var cartQuery = from ca in db.CartTbls.AsQueryable()
                                    where ca.CustomerID == cusID
                                    select new { ca.CartID, ca.TotalPrice };
                    double currentTotal = (double)cartQuery.FirstOrDefault().TotalPrice;
                    currentTotal = currentTotal + totalPrice;
                    cartTbl.TotalPrice = currentTotal;  //UPDATE LINE
                db.SubmitChanges();
                    csTbl.CartID = cartQuery.First().CartID;
                    csTbl.ShoeID = shoeID;
                    csTbl.Quantity = Convert.ToInt32(quantityDdl.Text);
                    db.CartShoeTbls.InsertOnSubmit(csTbl);
                    db.SubmitChanges();
                }
            }
            else
            {
                cartTbl.CustomerID = cusID;
                cartTbl.TotalPrice = totalPrice;
                db.CartTbls.InsertOnSubmit(cartTbl);
                csTbl.CartID = cartTbl.CartID;
                csTbl.ShoeID = shoeID;
                csTbl.Quantity = quantity;
                db.CartShoeTbls.InsertOnSubmit(csTbl);
                db.SubmitChanges();
            }
        }
        else 
        {
            Response.Write("<script>alert('Please login before you start to shopping !');</script>");
        }
    }

Linq to sql更新现在可以工作了

cartTbl永远不会设置为属于数据库的任何值。这是你创建的new cartTbl,它不在数据库中。您需要更改数据库中的内容以更新数据库。