Пример #1
0
        public void AddToCart(Album album)
        {
            // Get the matching cart and album instances
            var cartItem = _db.CartItems.SingleOrDefault(
                c => c.CartId == ShoppingCartId &&
                c.AlbumId == album.AlbumId);

            if (cartItem == null)
            {
                // TODO [EF] Swap to store generated key once we support identity pattern
                var nextCartItemId = _db.CartItems.Any()
                    ? _db.CartItems.Max(c => c.CartItemId) + 1
                    : 1;

                // Create a new cart item if no cart item exists
                cartItem = new CartItem
                {
                    CartItemId  = nextCartItemId,
                    AlbumId     = album.AlbumId,
                    CartId      = ShoppingCartId,
                    Count       = 1,
                    DateCreated = DateTime.Now
                };

                _db.CartItems.Add(cartItem);
            }
            else
            {
                // If the item does exist in the cart, then add one to the quantity
                cartItem.Count++;

                // TODO [EF] Remove this line once change detection is available
                _db.Entry(cartItem).SetState(EntityState.Modified);
            }
        }
Пример #2
0
 public void UpdateAlbum(int id, Album a)
 {
     db.Entry(a).State = EntityState.Modified;
     db.SaveChanges();
 }