public void AddToCart(Album album) { var cartItem = storeDB.Carts.SingleOrDefault( c => c.CartId == shoppingCartId && c.AlbumId == album.AlbumId); if (cartItem == null) { // Create a new cart item cartItem = new Cart { AlbumId = album.AlbumId, CartId = shoppingCartId, Count = 1, DateCreated = DateTime.Now }; storeDB.AddToCarts(cartItem); } else { // Add one to the quantity cartItem.Count++; } // Save it storeDB.SaveChanges(); }
/// <summary> /// Create a new Cart object. /// </summary> /// <param name="recordId">Initial value of the RecordId property.</param> /// <param name="cartId">Initial value of the CartId property.</param> /// <param name="albumId">Initial value of the AlbumId property.</param> /// <param name="count">Initial value of the Count property.</param> /// <param name="dateCreated">Initial value of the DateCreated property.</param> public static Cart CreateCart(global::System.Int32 recordId, global::System.String cartId, global::System.Int32 albumId, global::System.Int32 count, global::System.DateTime dateCreated) { Cart cart = new Cart(); cart.RecordId = recordId; cart.CartId = cartId; cart.AlbumId = albumId; cart.Count = count; cart.DateCreated = dateCreated; 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); }