From fe9770cc805d1ccb6ff69f0c72071368d59b5fea Mon Sep 17 00:00:00 2001 From: rowanmiller Date: Tue, 25 Mar 2014 16:01:01 -0700 Subject: [PATCH] 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) --- .../Controllers/ShoppingCartController.cs | 2 +- .../Controllers/StoreManagerController.cs | 9 +--- src/MusicStore/Models/Artist.cs | 4 ++ .../Models/{Cart.cs => CartItem.cs} | 7 +-- src/MusicStore/Models/Genre.cs | 3 ++ src/MusicStore/Models/MusicStoreContext.cs | 45 ++++++------------- src/MusicStore/Models/Order.cs | 1 + src/MusicStore/Models/SampleData.cs | 10 ++--- src/MusicStore/Models/ShoppingCart.cs | 14 +++--- .../ViewModels/ShoppingCartViewModel.cs | 2 +- 10 files changed, 40 insertions(+), 57 deletions(-) rename src/MusicStore/Models/{Cart.cs => CartItem.cs} (84%) diff --git a/src/MusicStore/Controllers/ShoppingCartController.cs b/src/MusicStore/Controllers/ShoppingCartController.cs index 04e123dfc1..68226d165d 100644 --- a/src/MusicStore/Controllers/ShoppingCartController.cs +++ b/src/MusicStore/Controllers/ShoppingCartController.cs @@ -64,7 +64,7 @@ namespace MusicStore.Controllers // Get the name of the album to display confirmation // 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; // Remove from cart diff --git a/src/MusicStore/Controllers/StoreManagerController.cs b/src/MusicStore/Controllers/StoreManagerController.cs index 5823f6e8a9..76a9a4d073 100644 --- a/src/MusicStore/Controllers/StoreManagerController.cs +++ b/src/MusicStore/Controllers/StoreManagerController.cs @@ -19,10 +19,7 @@ namespace MusicStore.Controllers public IActionResult Index() { - //Bug: Include needs EF. - //var albums = db.Albums.Include(a => a.Genre).Include(a => a.Artist) - // .OrderBy(a => a.Price); - + // TODO [EF] Swap to native support for loading related data when available var albums = db.Albums; foreach (var album in albums) { @@ -38,8 +35,6 @@ namespace MusicStore.Controllers 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); if (album == null) { @@ -86,8 +81,6 @@ namespace MusicStore.Controllers 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); if (album == null) diff --git a/src/MusicStore/Models/Artist.cs b/src/MusicStore/Models/Artist.cs index f893ae2497..c398d51590 100644 --- a/src/MusicStore/Models/Artist.cs +++ b/src/MusicStore/Models/Artist.cs @@ -1,10 +1,14 @@ // 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 { public class Artist { public int ArtistId { get; set; } + + [Required] public string Name { get; set; } } } \ No newline at end of file diff --git a/src/MusicStore/Models/Cart.cs b/src/MusicStore/Models/CartItem.cs similarity index 84% rename from src/MusicStore/Models/Cart.cs rename to src/MusicStore/Models/CartItem.cs index 4cbfd73e2c..a986c2e4dd 100644 --- a/src/MusicStore/Models/Cart.cs +++ b/src/MusicStore/Models/CartItem.cs @@ -5,10 +5,11 @@ using System.ComponentModel.DataAnnotations; namespace MusicStore.Models { - public class Cart + public class CartItem { - [Key] - public int RecordId { get; set; } + public int CartItemId { get; set; } + + [Required] public string CartId { get; set; } public int AlbumId { get; set; } public int Count { get; set; } diff --git a/src/MusicStore/Models/Genre.cs b/src/MusicStore/Models/Genre.cs index f3322d6a96..1680d712c4 100644 --- a/src/MusicStore/Models/Genre.cs +++ b/src/MusicStore/Models/Genre.cs @@ -1,12 +1,15 @@ // 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.ComponentModel.DataAnnotations; namespace MusicStore.Models { public class Genre { public int GenreId { get; set; } + + [Required] public string Name { get; set; } public string Description { get; set; } public List Albums { get; set; } diff --git a/src/MusicStore/Models/MusicStoreContext.cs b/src/MusicStore/Models/MusicStoreContext.cs index 2b98757aad..625d69543e 100644 --- a/src/MusicStore/Models/MusicStoreContext.cs +++ b/src/MusicStore/Models/MusicStoreContext.cs @@ -9,49 +9,30 @@ namespace MusicStore.Models { public class MusicStoreContext : EntityContext { - private static EntityConfiguration _config; - public EntitySet Albums { get; set; } public EntitySet Artists { get; set; } public EntitySet Orders { get; set; } public EntitySet Genres { get; set; } - public EntitySet Carts { get; set; } + public EntitySet Carts { get; set; } public EntitySet OrderDetails { get; set; } - public MusicStoreContext() - : 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() + protected override void OnConfiguring(EntityConfigurationBuilder builder) { - if (_config == null) - { - var model = new Model(); - var modelBuilder = new ModelBuilder(model); - modelBuilder.Entity().Key(a => a.AlbumId); - modelBuilder.Entity().Key(a => a.ArtistId); - modelBuilder.Entity().Key(o => o.OrderId).StorageName("[Order]"); - modelBuilder.Entity().Key(g => g.GenreId); - modelBuilder.Entity().Key(c => c.RecordId); - modelBuilder.Entity().Key(o => o.OrderDetailId); - new SimpleTemporaryConvention().Apply(model); - - var builder = new EntityConfigurationBuilder(); - - // TODO [EF] Remove once SQL Client is available on K10 #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 - builder.UseDataStore(new InMemoryDataStore()); + builder.UseDataStore(new InMemoryDataStore()); #endif - builder.UseModel(model); + } - _config = builder.BuildConfiguration(); - } - - return _config; + protected override void OnModelCreating(ModelBuilder builder) + { + builder.Entity().Key(a => a.AlbumId); + builder.Entity().Key(a => a.ArtistId); + builder.Entity().Key(o => o.OrderId).StorageName("[Order]"); + builder.Entity().Key(g => g.GenreId); + builder.Entity().Key(c => c.CartItemId); + builder.Entity().Key(o => o.OrderDetailId); } } } \ No newline at end of file diff --git a/src/MusicStore/Models/Order.cs b/src/MusicStore/Models/Order.cs index 3170278aa7..eca87275ee 100644 --- a/src/MusicStore/Models/Order.cs +++ b/src/MusicStore/Models/Order.cs @@ -14,6 +14,7 @@ namespace MusicStore.Models [ScaffoldColumn(false)] public System.DateTime OrderDate { get; set; } + [Required] [ScaffoldColumn(false)] public string Username { get; set; } diff --git a/src/MusicStore/Models/SampleData.cs b/src/MusicStore/Models/SampleData.cs index db8d686e82..14bb4f0414 100644 --- a/src/MusicStore/Models/SampleData.cs +++ b/src/MusicStore/Models/SampleData.cs @@ -874,7 +874,7 @@ namespace MusicStore.Web.Models { 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)."; } @@ -918,7 +918,7 @@ CREATE TABLE [dbo].[Album]( [ArtistId] [int] NOT NULL, [Title] [nvarchar](160) NOT NULL, [Price] [numeric](18, 2) NOT NULL, - [AlbumArtUrl] [nvarchar](1024) NOT NULL + [AlbumArtUrl] [nvarchar](1024) NULL ) CREATE TABLE [dbo].[Artist]( @@ -926,8 +926,8 @@ CREATE TABLE [dbo].[Artist]( [Name] [nvarchar](max) NOT NULL ) -CREATE TABLE [dbo].[Cart]( - [RecordId] [int] NOT NULL, +CREATE TABLE [dbo].[CartItem]( + [CartItemId] [int] NOT NULL, [CartId] [nvarchar](max) NOT NULL, [AlbumId] [int] NOT NULL, [Count] [int] NOT NULL, @@ -937,7 +937,7 @@ CREATE TABLE [dbo].[Cart]( CREATE TABLE [dbo].[Genre]( [GenreId] [int] NOT NULL, [Name] [nvarchar](max) NOT NULL, - [Description] [nvarchar](max) NOT NULL + [Description] [nvarchar](max) NULL ) CREATE TABLE [dbo].[Order]( diff --git a/src/MusicStore/Models/ShoppingCart.cs b/src/MusicStore/Models/ShoppingCart.cs index 67c8740c18..cbc482ecd1 100644 --- a/src/MusicStore/Models/ShoppingCart.cs +++ b/src/MusicStore/Models/ShoppingCart.cs @@ -45,14 +45,14 @@ namespace MusicStore.Models if (cartItem == null) { // TODO [EF] Swap to store generated key once we support identity pattern - var nextRecordId = _db.Carts.Any() - ? _db.Carts.Max(c => c.RecordId) + 1 + var nextCartItemId = _db.Carts.Any() + ? _db.Carts.Max(c => c.CartItemId) + 1 : 1; // Create a new cart item if no cart item exists - cartItem = new Cart + cartItem = new CartItem { - RecordId = nextRecordId, + CartItemId = nextCartItemId, AlbumId = album.AlbumId, CartId = ShoppingCartId, Count = 1, @@ -76,7 +76,7 @@ namespace MusicStore.Models // Get the cart var cartItem = _db.Carts.Single( cart => cart.CartId == ShoppingCartId - && cart.RecordId == id); + && cart.CartItemId == id); int itemCount = 0; @@ -113,7 +113,7 @@ namespace MusicStore.Models } - public List GetCartItems() + public List GetCartItems() { 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); - foreach (Cart item in shoppingCart) + foreach (CartItem item in shoppingCart) { item.CartId = userName; } diff --git a/src/MusicStore/ViewModels/ShoppingCartViewModel.cs b/src/MusicStore/ViewModels/ShoppingCartViewModel.cs index 04cb00eb0c..3948885965 100644 --- a/src/MusicStore/ViewModels/ShoppingCartViewModel.cs +++ b/src/MusicStore/ViewModels/ShoppingCartViewModel.cs @@ -7,7 +7,7 @@ namespace MusicStore.ViewModels { public class ShoppingCartViewModel { - public List CartItems { get; set; } + public List CartItems { get; set; } public decimal CartTotal { get; set; } } }