[英]Linq to sql update does now work
我正在嘗試使用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>");
}
}
cartTbl
永遠不會設置為屬於數據庫的任何值。 這是您創建的new
cartTbl,並且不在數據庫中。 你需要改變的東西,是在數據庫中更新數據庫。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.