私のAddToCartアクションメソッドについて考えてみましょう。私の質問は、私のコメント// 3から起こることに関係しています。保存または更新してください.CartItem:Linqは2回セーブですか?
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddToCart(
[Bind("ShoppingCartItemProductId,ShoppingCartItemQuantity")]
ViewModelAddToCart model)
{
// Initialize session to enable SessionId
// TODO: Check if cookie exists
HttpContext.Session.SetString("_Name", "MyStore");
DateTime dt = DateTime.Now;
string SessionId = HttpContext.Session.Id;
var ShoppingCart = new ShoppingCart()
{
SessionId = SessionId,
CreateDate = dt,
Title = "My shopping cart (" + dt.ToShortDateString() + ")"
// (not in use yet) CustomerId = 0
};
var ShoppingCartItem = new ShoppingCartItem()
{
ProductId = model.ShoppingCartItemProductId,
Quantity = model.ShoppingCartItemQuantity
};
// 1. If a ShoppingCart exist with current SessionId, get ShoppingCartId from
// that one and use it in the ShoppingCartItem
if (ModelState.IsValid)
{
// Query for ShoppingCart containing current SessionId.
var cartInfo =
(from Cart in _context.ShoppingCarts
where Cart.SessionId == SessionId
select new { TempId = Cart.Id })
.SingleOrDefault();
if (cartInfo != null)
{
// *** [ Use existing ShoppingCart ] ***
// 2. Set ShoppingCartId for ShoppingCartItem
ShoppingCartItem.ShoppingCartId = cartInfo.TempId;
}
else
{
// *** [ Create a new shoppingCart ] ***
_context.ShoppingCarts.Add(ShoppingCart);
await _context.SaveChangesAsync();
// 2. Set ShoppingCartId in ShoppingCartItem
ShoppingCartItem.ShoppingCartId = ShoppingCart.Id;
}
// 3. save or update shoppingCartItem
// Query for ShoppingCartItem containing current ProductId
var cartItemInfo =
(from CartItem in _context.ShoppingCartItems
where CartItem.ProductId == model.ShoppingCartItemProductId
select new { TempQty = CartItem.Quantity })
.FirstOrDefault();
if (cartItemInfo != null)
{
// 4. update quantity for existing ShoppingCartItem
ShoppingCartItem.Quantity += cartItemInfo.TempQty; // current quantity + added quantity
_context.ShoppingCartItems.Update(ShoppingCartItem);
}
else
{
// 4. create a new shoppingCartItem
_context.ShoppingCartItems.Add(ShoppingCartItem);
}
await _context.SaveChangesAsync();
return RedirectToAction("Index", "Home");
}
return View("Index", "Home");
}
ショッピングカートが正しく保存されています。現在のSessionIdを含んでいる場合は、既存のShoppingCartIdと共にShoppingCartItemを追加します。
ShoppingCartItemsは、一方で、正しく行われていません。それらは保存されますが、商品が複数回追加されると、新しい数量で新しいShoppingCartItemが作成されます。現在の製品の最初のテーブルレコードがチェックされ、その数量は1です。次に、1が追加され、新しいShoppingCartItemの数量2が作成されます。もちろん
//Product gets added first time:
[Id] [ProductId] [Quantity] [ShoppingCartId]
[6092] [5] [1] [6091]
//The same product is added again:
[Id] [ProductId] [Quantity] [ShoppingCartId]
[6092] [5] [1] [6091]
[6093] [5] [2] [6091]
は、望ましい行動は次のようになります::
//Product gets added first time:
[Id] [ProductId] [Quantity] [ShoppingCartId]
[6092] [5] [1] [6091]
//The same product is added again:
[Id] [ProductId] [Quantity] [ShoppingCartId]
[6092] [5] [2] [6091]
私が間違っているのは何
説明するために、これは、テーブルShoppingCartItemsのデータビューがありますか?
これは、_context.SaveChangesAsync();を待って、1つの場所に保存しているようです。それがアクションにリダイレクトされる直前(//既存のShoppingCartItemの数量を更新した後5.)。私はテーブルの正しいレコードには触れていないと思う。どのようにして正しい行を得ることができますか? – Stian