Use OnModelCreating & OnConfiguring (plus some other cleanup)

Swapping context to use OnModelCreating & OnConfiguring now that models
are cached
Adding some [Required] annotations to properties that are really
required in the domain model
Renaming Cart => CartItem because that's what it really is :)
Allowing database to hold some null data now that we can materialize it
(although we can't save it yet)
This commit is contained in:
rowanmiller 2014-03-25 16:01:01 -07:00
parent 965046813e
commit fe9770cc80
10 changed files with 40 additions and 57 deletions

View File

@ -64,7 +64,7 @@ namespace MusicStore.Controllers
// Get the name of the album to display confirmation // Get the name of the album to display confirmation
// TODO [EF] Turn into one query once query of related data is enabled // TODO [EF] Turn into one query once query of related data is enabled
int albumId = db.Carts.Single(item => item.RecordId == id).AlbumId; int albumId = db.Carts.Single(item => item.CartItemId == id).AlbumId;
string albumName = db.Albums.Single(a => a.AlbumId == albumId).Title; string albumName = db.Albums.Single(a => a.AlbumId == albumId).Title;
// Remove from cart // Remove from cart

View File

@ -19,10 +19,7 @@ namespace MusicStore.Controllers
public IActionResult Index() public IActionResult Index()
{ {
//Bug: Include needs EF. // TODO [EF] Swap to native support for loading related data when available
//var albums = db.Albums.Include(a => a.Genre).Include(a => a.Artist)
// .OrderBy(a => a.Price);
var albums = db.Albums; var albums = db.Albums;
foreach (var album in albums) foreach (var album in albums)
{ {
@ -38,8 +35,6 @@ namespace MusicStore.Controllers
public IActionResult Details(int id = 0) public IActionResult Details(int id = 0)
{ {
//Bug: Find needs EF
//Album album = db.Albums.Find(id);
Album album = db.Albums.Single(a => a.AlbumId == id); Album album = db.Albums.Single(a => a.AlbumId == id);
if (album == null) if (album == null)
{ {
@ -86,8 +81,6 @@ namespace MusicStore.Controllers
public IActionResult Edit(int id = 0) public IActionResult Edit(int id = 0)
{ {
//Bug: Need EF to implement Find
//Album album = db.Albums.Find(id);
Album album = db.Albums.Single(a => a.AlbumId == id); Album album = db.Albums.Single(a => a.AlbumId == id);
if (album == null) if (album == null)

View File

@ -1,10 +1,14 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.ComponentModel.DataAnnotations;
namespace MusicStore.Models namespace MusicStore.Models
{ {
public class Artist public class Artist
{ {
public int ArtistId { get; set; } public int ArtistId { get; set; }
[Required]
public string Name { get; set; } public string Name { get; set; }
} }
} }

View File

@ -5,10 +5,11 @@ using System.ComponentModel.DataAnnotations;
namespace MusicStore.Models namespace MusicStore.Models
{ {
public class Cart public class CartItem
{ {
[Key] public int CartItemId { get; set; }
public int RecordId { get; set; }
[Required]
public string CartId { get; set; } public string CartId { get; set; }
public int AlbumId { get; set; } public int AlbumId { get; set; }
public int Count { get; set; } public int Count { get; set; }

View File

@ -1,12 +1,15 @@
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace MusicStore.Models namespace MusicStore.Models
{ {
public class Genre public class Genre
{ {
public int GenreId { get; set; } public int GenreId { get; set; }
[Required]
public string Name { get; set; } public string Name { get; set; }
public string Description { get; set; } public string Description { get; set; }
public List<Album> Albums { get; set; } public List<Album> Albums { get; set; }

View File

@ -9,49 +9,30 @@ namespace MusicStore.Models
{ {
public class MusicStoreContext : EntityContext public class MusicStoreContext : EntityContext
{ {
private static EntityConfiguration _config;
public EntitySet<Album> Albums { get; set; } public EntitySet<Album> Albums { get; set; }
public EntitySet<Artist> Artists { get; set; } public EntitySet<Artist> Artists { get; set; }
public EntitySet<Order> Orders { get; set; } public EntitySet<Order> Orders { get; set; }
public EntitySet<Genre> Genres { get; set; } public EntitySet<Genre> Genres { get; set; }
public EntitySet<Cart> Carts { get; set; } public EntitySet<CartItem> Carts { get; set; }
public EntitySet<OrderDetail> OrderDetails { get; set; } public EntitySet<OrderDetail> OrderDetails { get; set; }
public MusicStoreContext() protected override void OnConfiguring(EntityConfigurationBuilder builder)
: base(GetConfiguration())
{ }
// TODO Not using OnModelCreating and OnConfiguring because model is not cached and that breaks InMemoryDataStore
// because IEntityType is a different instance for the same type between context instances
private static EntityConfiguration GetConfiguration()
{ {
if (_config == null)
{
var model = new Model();
var modelBuilder = new ModelBuilder(model);
modelBuilder.Entity<Album>().Key(a => a.AlbumId);
modelBuilder.Entity<Artist>().Key(a => a.ArtistId);
modelBuilder.Entity<Order>().Key(o => o.OrderId).StorageName("[Order]");
modelBuilder.Entity<Genre>().Key(g => g.GenreId);
modelBuilder.Entity<Cart>().Key(c => c.RecordId);
modelBuilder.Entity<OrderDetail>().Key(o => o.OrderDetailId);
new SimpleTemporaryConvention().Apply(model);
var builder = new EntityConfigurationBuilder();
// TODO [EF] Remove once SQL Client is available on K10
#if NET45 #if NET45
builder.UseSqlServer(@"Server=(localdb)\v11.0;Database=MusicStore;Trusted_Connection=True;"); builder.UseSqlServer(@"Server=(localdb)\v11.0;Database=MusicStore;Trusted_Connection=True;");
#else #else
builder.UseDataStore(new InMemoryDataStore()); builder.UseDataStore(new InMemoryDataStore());
#endif #endif
builder.UseModel(model); }
_config = builder.BuildConfiguration(); protected override void OnModelCreating(ModelBuilder builder)
} {
builder.Entity<Album>().Key(a => a.AlbumId);
return _config; builder.Entity<Artist>().Key(a => a.ArtistId);
builder.Entity<Order>().Key(o => o.OrderId).StorageName("[Order]");
builder.Entity<Genre>().Key(g => g.GenreId);
builder.Entity<CartItem>().Key(c => c.CartItemId);
builder.Entity<OrderDetail>().Key(o => o.OrderDetailId);
} }
} }
} }

View File

@ -14,6 +14,7 @@ namespace MusicStore.Models
[ScaffoldColumn(false)] [ScaffoldColumn(false)]
public System.DateTime OrderDate { get; set; } public System.DateTime OrderDate { get; set; }
[Required]
[ScaffoldColumn(false)] [ScaffoldColumn(false)]
public string Username { get; set; } public string Username { get; set; }

View File

@ -874,7 +874,7 @@ namespace MusicStore.Web.Models
{ {
genre.GenreId = genreId++; genre.GenreId = genreId++;
// TODO [EF] Remove when null values are supported // TODO [EF] Remove when null values are supported by update pipeline
genre.Description = genre.Name + " is great music (if you like it)."; genre.Description = genre.Name + " is great music (if you like it).";
} }
@ -918,7 +918,7 @@ CREATE TABLE [dbo].[Album](
[ArtistId] [int] NOT NULL, [ArtistId] [int] NOT NULL,
[Title] [nvarchar](160) NOT NULL, [Title] [nvarchar](160) NOT NULL,
[Price] [numeric](18, 2) NOT NULL, [Price] [numeric](18, 2) NOT NULL,
[AlbumArtUrl] [nvarchar](1024) NOT NULL [AlbumArtUrl] [nvarchar](1024) NULL
) )
CREATE TABLE [dbo].[Artist]( CREATE TABLE [dbo].[Artist](
@ -926,8 +926,8 @@ CREATE TABLE [dbo].[Artist](
[Name] [nvarchar](max) NOT NULL [Name] [nvarchar](max) NOT NULL
) )
CREATE TABLE [dbo].[Cart]( CREATE TABLE [dbo].[CartItem](
[RecordId] [int] NOT NULL, [CartItemId] [int] NOT NULL,
[CartId] [nvarchar](max) NOT NULL, [CartId] [nvarchar](max) NOT NULL,
[AlbumId] [int] NOT NULL, [AlbumId] [int] NOT NULL,
[Count] [int] NOT NULL, [Count] [int] NOT NULL,
@ -937,7 +937,7 @@ CREATE TABLE [dbo].[Cart](
CREATE TABLE [dbo].[Genre]( CREATE TABLE [dbo].[Genre](
[GenreId] [int] NOT NULL, [GenreId] [int] NOT NULL,
[Name] [nvarchar](max) NOT NULL, [Name] [nvarchar](max) NOT NULL,
[Description] [nvarchar](max) NOT NULL [Description] [nvarchar](max) NULL
) )
CREATE TABLE [dbo].[Order]( CREATE TABLE [dbo].[Order](

View File

@ -45,14 +45,14 @@ namespace MusicStore.Models
if (cartItem == null) if (cartItem == null)
{ {
// TODO [EF] Swap to store generated key once we support identity pattern // TODO [EF] Swap to store generated key once we support identity pattern
var nextRecordId = _db.Carts.Any() var nextCartItemId = _db.Carts.Any()
? _db.Carts.Max(c => c.RecordId) + 1 ? _db.Carts.Max(c => c.CartItemId) + 1
: 1; : 1;
// Create a new cart item if no cart item exists // Create a new cart item if no cart item exists
cartItem = new Cart cartItem = new CartItem
{ {
RecordId = nextRecordId, CartItemId = nextCartItemId,
AlbumId = album.AlbumId, AlbumId = album.AlbumId,
CartId = ShoppingCartId, CartId = ShoppingCartId,
Count = 1, Count = 1,
@ -76,7 +76,7 @@ namespace MusicStore.Models
// Get the cart // Get the cart
var cartItem = _db.Carts.Single( var cartItem = _db.Carts.Single(
cart => cart.CartId == ShoppingCartId cart => cart.CartId == ShoppingCartId
&& cart.RecordId == id); && cart.CartItemId == id);
int itemCount = 0; int itemCount = 0;
@ -113,7 +113,7 @@ namespace MusicStore.Models
} }
public List<Cart> GetCartItems() public List<CartItem> GetCartItems()
{ {
return _db.Carts.Where(cart => cart.CartId == ShoppingCartId).ToList(); return _db.Carts.Where(cart => cart.CartId == ShoppingCartId).ToList();
} }
@ -221,7 +221,7 @@ namespace MusicStore.Models
{ {
var shoppingCart = _db.Carts.Where(c => c.CartId == ShoppingCartId); var shoppingCart = _db.Carts.Where(c => c.CartId == ShoppingCartId);
foreach (Cart item in shoppingCart) foreach (CartItem item in shoppingCart)
{ {
item.CartId = userName; item.CartId = userName;
} }

View File

@ -7,7 +7,7 @@ namespace MusicStore.ViewModels
{ {
public class ShoppingCartViewModel public class ShoppingCartViewModel
{ {
public List<Cart> CartItems { get; set; } public List<CartItem> CartItems { get; set; }
public decimal CartTotal { get; set; } public decimal CartTotal { get; set; }
} }
} }