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
|
else
|
||||||
{
|
{
|
||||||
// TODO [EF] Swap to store generated identity key when supported
|
order.Username = Context.User.Identity.GetUserName();
|
||||||
var nextId = db.Orders.Any()
|
|
||||||
? db.Orders.Max(o => o.OrderId) + 1
|
|
||||||
: 1;
|
|
||||||
|
|
||||||
order.OrderId = nextId;
|
|
||||||
order.Username = this.Context.User.Identity.GetUserName();
|
|
||||||
order.OrderDate = DateTime.Now;
|
order.OrderDate = DateTime.Now;
|
||||||
|
|
||||||
//Add the Order
|
//Add the Order
|
||||||
|
|
@ -82,7 +76,7 @@ namespace MusicStore.Controllers
|
||||||
// Validate customer owns this order
|
// Validate customer owns this order
|
||||||
bool isValid = db.Orders.Any(
|
bool isValid = db.Orders.Any(
|
||||||
o => o.OrderId == id &&
|
o => o.OrderId == id &&
|
||||||
o.Username == this.Context.User.Identity.GetUserName());
|
o.Username == Context.User.Identity.GetUserName());
|
||||||
|
|
||||||
if (isValid)
|
if (isValid)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
using Microsoft.AspNet.Mvc;
|
using Microsoft.AspNet.Mvc;
|
||||||
using Microsoft.AspNet.Mvc.ModelBinding;
|
|
||||||
using Microsoft.AspNet.Mvc.Rendering;
|
using Microsoft.AspNet.Mvc.Rendering;
|
||||||
using Microsoft.Data.Entity;
|
using Microsoft.Data.Entity;
|
||||||
using MusicStore.Models;
|
using MusicStore.Models;
|
||||||
|
|
@ -66,12 +65,6 @@ namespace MusicStore.Controllers
|
||||||
{
|
{
|
||||||
if (ModelState.IsValid)
|
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.Albums.Add(album);
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
return RedirectToAction("Index");
|
return RedirectToAction("Index");
|
||||||
|
|
@ -92,6 +85,7 @@ namespace MusicStore.Controllers
|
||||||
{
|
{
|
||||||
return new HttpStatusCodeResult(404);
|
return new HttpStatusCodeResult(404);
|
||||||
}
|
}
|
||||||
|
|
||||||
ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
|
ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
|
||||||
ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
|
ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
|
||||||
return View(album);
|
return View(album);
|
||||||
|
|
@ -108,6 +102,7 @@ namespace MusicStore.Controllers
|
||||||
db.SaveChanges();
|
db.SaveChanges();
|
||||||
return RedirectToAction("Index");
|
return RedirectToAction("Index");
|
||||||
}
|
}
|
||||||
|
|
||||||
ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
|
ViewBag.GenreId = new SelectList(db.Genres, "GenreId", "Name", album.GenreId);
|
||||||
ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
|
ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
|
||||||
return View(album);
|
return View(album);
|
||||||
|
|
|
||||||
|
|
@ -32,15 +32,9 @@ namespace MusicStore.Models
|
||||||
|
|
||||||
if (cartItem == null)
|
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
|
// Create a new cart item if no cart item exists
|
||||||
cartItem = new CartItem
|
cartItem = new CartItem
|
||||||
{
|
{
|
||||||
CartItemId = nextCartItemId,
|
|
||||||
AlbumId = album.AlbumId,
|
AlbumId = album.AlbumId,
|
||||||
CartId = ShoppingCartId,
|
CartId = ShoppingCartId,
|
||||||
Count = 1,
|
Count = 1,
|
||||||
|
|
@ -53,9 +47,6 @@ namespace MusicStore.Models
|
||||||
{
|
{
|
||||||
// If the item does exist in the cart, then add one to the quantity
|
// If the item does exist in the cart, then add one to the quantity
|
||||||
cartItem.Count++;
|
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)
|
if (cartItem.Count > 1)
|
||||||
{
|
{
|
||||||
cartItem.Count--;
|
cartItem.Count--;
|
||||||
|
|
||||||
// TODO [EF] Remove this line once change detection is available
|
|
||||||
_db.ChangeTracker.Entry(cartItem).State = EntityState.Modified;
|
|
||||||
|
|
||||||
itemCount = cartItem.Count;
|
itemCount = cartItem.Count;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
|
@ -91,12 +78,7 @@ namespace MusicStore.Models
|
||||||
public void EmptyCart()
|
public void EmptyCart()
|
||||||
{
|
{
|
||||||
var cartItems = _db.CartItems.Where(cart => cart.CartId == ShoppingCartId);
|
var cartItems = _db.CartItems.Where(cart => cart.CartId == ShoppingCartId);
|
||||||
|
_db.CartItems.RemoveRange(cartItems);
|
||||||
foreach (var cartItem in cartItems)
|
|
||||||
{
|
|
||||||
// TODO [EF] Change to DbSet.Remove once querying attaches instances
|
|
||||||
_db.ChangeTracker.Entry(cartItem).State = EntityState.Deleted;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<CartItem> GetCartItems()
|
public List<CartItem> GetCartItems()
|
||||||
|
|
@ -145,11 +127,6 @@ namespace MusicStore.Models
|
||||||
|
|
||||||
var cartItems = GetCartItems();
|
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
|
// Iterate over the items in the cart, adding the order details for each
|
||||||
foreach (var item in cartItems)
|
foreach (var item in cartItems)
|
||||||
{
|
{
|
||||||
|
|
@ -158,7 +135,6 @@ namespace MusicStore.Models
|
||||||
|
|
||||||
var orderDetail = new OrderDetail
|
var orderDetail = new OrderDetail
|
||||||
{
|
{
|
||||||
OrderDetailId = nextId,
|
|
||||||
AlbumId = item.AlbumId,
|
AlbumId = item.AlbumId,
|
||||||
OrderId = order.OrderId,
|
OrderId = order.OrderId,
|
||||||
UnitPrice = album.Price,
|
UnitPrice = album.Price,
|
||||||
|
|
@ -169,8 +145,6 @@ namespace MusicStore.Models
|
||||||
orderTotal += (item.Count * album.Price);
|
orderTotal += (item.Count * album.Price);
|
||||||
|
|
||||||
_db.OrderDetails.Add(orderDetail);
|
_db.OrderDetails.Add(orderDetail);
|
||||||
|
|
||||||
nextId++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the order's total to the orderTotal count
|
// Set the order's total to the orderTotal count
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue