From 8c7bc1a9c85a6a2ba7f0d6087df1de20e4b84306 Mon Sep 17 00:00:00 2001 From: Praburaj Date: Thu, 3 Jul 2014 12:37:31 -0700 Subject: [PATCH] Auto id generation is available for entities Previously the sample had work arounds to generate an id itself while adding a new item into a table. Removing the work around. --- .../Controllers/CheckoutController.cs | 10 ++----- .../Controllers/StoreManagerController.cs | 9 ++---- src/MusicStore/Models/ShoppingCart.cs | 28 +------------------ 3 files changed, 5 insertions(+), 42 deletions(-) diff --git a/src/MusicStore/Controllers/CheckoutController.cs b/src/MusicStore/Controllers/CheckoutController.cs index 5e343d3388..02c6b01eba 100644 --- a/src/MusicStore/Controllers/CheckoutController.cs +++ b/src/MusicStore/Controllers/CheckoutController.cs @@ -44,13 +44,7 @@ namespace MusicStore.Controllers } else { - // TODO [EF] Swap to store generated identity key when supported - var nextId = db.Orders.Any() - ? db.Orders.Max(o => o.OrderId) + 1 - : 1; - - order.OrderId = nextId; - order.Username = this.Context.User.Identity.GetUserName(); + order.Username = Context.User.Identity.GetUserName(); order.OrderDate = DateTime.Now; //Add the Order @@ -82,7 +76,7 @@ namespace MusicStore.Controllers // Validate customer owns this order bool isValid = db.Orders.Any( o => o.OrderId == id && - o.Username == this.Context.User.Identity.GetUserName()); + o.Username == Context.User.Identity.GetUserName()); if (isValid) { diff --git a/src/MusicStore/Controllers/StoreManagerController.cs b/src/MusicStore/Controllers/StoreManagerController.cs index 43fa99ade3..a714c507d2 100644 --- a/src/MusicStore/Controllers/StoreManagerController.cs +++ b/src/MusicStore/Controllers/StoreManagerController.cs @@ -1,5 +1,4 @@ using Microsoft.AspNet.Mvc; -using Microsoft.AspNet.Mvc.ModelBinding; using Microsoft.AspNet.Mvc.Rendering; using Microsoft.Data.Entity; using MusicStore.Models; @@ -66,12 +65,6 @@ namespace MusicStore.Controllers { if (ModelState.IsValid) { - // TODO [EF] Swap to store generated identity key when supported - var nextId = db.Albums.Any() - ? db.Albums.Max(o => o.AlbumId) + 1 - : 1; - - album.AlbumId = nextId; db.Albums.Add(album); db.SaveChanges(); return RedirectToAction("Index"); @@ -92,6 +85,7 @@ namespace MusicStore.Controllers { return new HttpStatusCodeResult(404); } + ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); return View(album); @@ -108,6 +102,7 @@ namespace MusicStore.Controllers db.SaveChanges(); return RedirectToAction("Index"); } + ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId); ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId); return View(album); diff --git a/src/MusicStore/Models/ShoppingCart.cs b/src/MusicStore/Models/ShoppingCart.cs index 69223596a3..d15b7ef705 100644 --- a/src/MusicStore/Models/ShoppingCart.cs +++ b/src/MusicStore/Models/ShoppingCart.cs @@ -32,15 +32,9 @@ namespace MusicStore.Models 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, @@ -53,9 +47,6 @@ namespace MusicStore.Models { // 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.ChangeTracker.Entry(cartItem).State = EntityState.Modified; } } @@ -73,10 +64,6 @@ namespace MusicStore.Models if (cartItem.Count > 1) { cartItem.Count--; - - // TODO [EF] Remove this line once change detection is available - _db.ChangeTracker.Entry(cartItem).State = EntityState.Modified; - itemCount = cartItem.Count; } else @@ -91,12 +78,7 @@ namespace MusicStore.Models public void EmptyCart() { var cartItems = _db.CartItems.Where(cart => cart.CartId == ShoppingCartId); - - foreach (var cartItem in cartItems) - { - // TODO [EF] Change to DbSet.Remove once querying attaches instances - _db.ChangeTracker.Entry(cartItem).State = EntityState.Deleted; - } + _db.CartItems.RemoveRange(cartItems); } public List GetCartItems() @@ -145,11 +127,6 @@ namespace MusicStore.Models var cartItems = GetCartItems(); - // TODO [EF] Swap to store generated identity key when supported - var nextId = _db.OrderDetails.Any() - ? _db.OrderDetails.Max(o => o.OrderDetailId) + 1 - : 1; - // Iterate over the items in the cart, adding the order details for each foreach (var item in cartItems) { @@ -158,7 +135,6 @@ namespace MusicStore.Models var orderDetail = new OrderDetail { - OrderDetailId = nextId, AlbumId = item.AlbumId, OrderId = order.OrderId, UnitPrice = album.Price, @@ -169,8 +145,6 @@ namespace MusicStore.Models orderTotal += (item.Count * album.Price); _db.OrderDetails.Add(orderDetail); - - nextId++; } // Set the order's total to the orderTotal count