Exemple #1
0
        public void AddToCart(Book book, int qty)
        {
            // Get the matching cart and book instances
            var cartItem = dbc.Carts.SingleOrDefault(
                c => c.CartId == ShoppingCartId &&
                c.BookId == book.BookId);

            if (cartItem == null)
            {
                // Create a new cart item if no cart item exists
                cartItem = new Cart
                {
                    BookId      = book.BookId,
                    CartId      = ShoppingCartId,
                    Count       = qty,
                    DateCreated = DateTime.Now
                };
                dbc.Carts.Add(cartItem);
            }
            else
            {
                // If the item does exist in the cart,
                // then add one to the quantity
                cartItem.Count += qty;
            }
            // Save changes
            dbc.SaveChanges();
        }