public void AddToCart(int pid) { // Get the matching cart and album instances var cartItem = context.Carts.SingleOrDefault(c => c.UniqueIdentifier == ShoppingCartId && c.ProductId == pid); if (cartItem == null) { cartItem = new Cart { ProductId = pid, UniqueIdentifier = ShoppingCartId, Quantity = 1, DateCreated = DateTime.Now }; context.Carts.AddObject(cartItem); } else { // If the item does exist in the cart, then add one to the quantity cartItem.Quantity++; } // Save changes context.SaveChanges(); }
/// <summary> /// Create a new Cart object. /// </summary> /// <param name="cartId">Initial value of the CartId property.</param> /// <param name="uniqueIdentifier">Initial value of the UniqueIdentifier property.</param> /// <param name="productId">Initial value of the ProductId property.</param> public static Cart CreateCart(global::System.Int32 cartId, global::System.Guid uniqueIdentifier, global::System.Int32 productId) { Cart cart = new Cart(); cart.CartId = cartId; cart.UniqueIdentifier = uniqueIdentifier; cart.ProductId = productId; return cart; }
/// <summary> /// Deprecated Method for adding a new object to the Carts EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToCarts(Cart cart) { base.AddObject("Carts", cart); }