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.
This commit is contained in:
parent
04de187189
commit
8c7bc1a9c8
|
|
@ -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)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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<CartItem> 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
|
||||
|
|
|
|||
Loading…
Reference in New Issue