diff --git a/src/MusicStore/Controllers/HomeController.cs b/src/MusicStore/Controllers/HomeController.cs index b91b6c64ed..37c7d4367f 100644 --- a/src/MusicStore/Controllers/HomeController.cs +++ b/src/MusicStore/Controllers/HomeController.cs @@ -1,9 +1,9 @@ -using Microsoft.Framework.Cache.Memory; -using Microsoft.AspNet.Mvc; -using MusicStore.Models; -using System; +using System; using System.Collections.Generic; using System.Linq; +using Microsoft.Framework.Cache.Memory; +using Microsoft.AspNet.Mvc; +using MusicStore.Models; namespace MusicStore.Controllers { diff --git a/src/MusicStore/Controllers/StoreController.cs b/src/MusicStore/Controllers/StoreController.cs index 9b23a3764e..6da044bd15 100644 --- a/src/MusicStore/Controllers/StoreController.cs +++ b/src/MusicStore/Controllers/StoreController.cs @@ -1,8 +1,8 @@ -using Microsoft.Framework.Cache.Memory; -using Microsoft.AspNet.Mvc; -using MusicStore.Models; -using System; +using System; using System.Linq; +using Microsoft.AspNet.Mvc; +using Microsoft.Framework.Cache.Memory; +using MusicStore.Models; namespace MusicStore.Controllers { @@ -33,11 +33,7 @@ namespace MusicStore.Controllers public IActionResult Browse(string genre) { // Retrieve Genre genre and its Associated associated Albums albums from database - - // TODO [EF] Swap to native support for loading related data when available - var genreModel = db.Genres.Single(g => g.Name == genre); - genreModel.Albums = db.Albums.Where(a => a.GenreId == genreModel.GenreId).ToList(); - + var genreModel = db.Genres.Include(g => g.Albums).Where(g => g.Name == genre).FirstOrDefault(); return View(genreModel); } @@ -48,11 +44,7 @@ namespace MusicStore.Controllers //Remove it from cache if not retrieved in last 10 minutes context.SetSlidingExpiration(TimeSpan.FromMinutes(10)); - var albumData = db.Albums.Single(a => a.AlbumId == id); - - // TODO [EF] We don't query related data as yet. We have to populate this until we do automatically. - albumData.Genre = db.Genres.Single(g => g.GenreId == albumData.GenreId); - albumData.Artist = db.Artists.Single(a => a.ArtistId == albumData.ArtistId); + var albumData = db.Albums.Where(a => a.AlbumId == id).Include(a => a.Artist).Include(a => a.Genre).ToList().FirstOrDefault(); return albumData; }); diff --git a/src/MusicStore/Models/MusicStoreContext.cs b/src/MusicStore/Models/MusicStoreContext.cs index 5f5a51b154..58a6ec2281 100644 --- a/src/MusicStore/Models/MusicStoreContext.cs +++ b/src/MusicStore/Models/MusicStoreContext.cs @@ -9,11 +9,6 @@ namespace MusicStore.Models public class MusicStoreContext : IdentityDbContext { - public MusicStoreContext() - { - - } - public DbSet Albums { get; set; } public DbSet Artists { get; set; } public DbSet Orders { get; set; } @@ -34,8 +29,13 @@ namespace MusicStore.Models builder.Entity().Property(a => a.ArtistId).GenerateValueOnAdd(generateValue: false); builder.Entity().Property(g => g.GenreId).GenerateValueOnAdd(generateValue: false); + // TODO: Remove this once convention-based relations work again + builder.Entity().ManyToOne(a => a.Artist); + builder.Entity().ManyToOne(a => a.Genre, g => g.Albums); + builder.Entity().OneToMany(o => o.OrderDetails); + builder.Entity().OneToMany(a => a.OrderDetails, od => od.Album); + base.OnModelCreating(builder); } } - } \ No newline at end of file diff --git a/src/MusicStore/Models/SampleData.cs b/src/MusicStore/Models/SampleData.cs index 0ad525c118..ac3ec069dc 100644 --- a/src/MusicStore/Models/SampleData.cs +++ b/src/MusicStore/Models/SampleData.cs @@ -17,7 +17,7 @@ namespace MusicStore.Models const string defaultAdminUserName = "DefaultAdminUserName"; const string defaultAdminPassword = "defaultAdminPassword"; - public static async Task InitializeMusicStoreDatabaseAsync(IServiceProvider serviceProvider) + public static async Task InitializeMusicStoreDatabaseAsync(IServiceProvider serviceProvider, bool createUsers = true) { using (var db = serviceProvider.GetService()) { @@ -27,13 +27,19 @@ namespace MusicStore.Models if (await sqlServerDatabase.EnsureCreatedAsync()) { await InsertTestData(serviceProvider); - await CreateAdminUser(serviceProvider); + if (createUsers) + { + await CreateAdminUser(serviceProvider); + } } } else { await InsertTestData(serviceProvider); - await CreateAdminUser(serviceProvider); + if (createUsers) + { + await CreateAdminUser(serviceProvider); + } } } } diff --git a/src/MusicStore/StartupNtlmAuthentication.cs b/src/MusicStore/StartupNtlmAuthentication.cs index 6cb7266a3c..d84f2d4ec8 100644 --- a/src/MusicStore/StartupNtlmAuthentication.cs +++ b/src/MusicStore/StartupNtlmAuthentication.cs @@ -10,7 +10,6 @@ using Microsoft.AspNet.Server.WebListener; using System.Security.Claims; using System.Security.Principal; using Microsoft.Framework.Cache.Memory; -using Microsoft.AspNet.Identity; namespace MusicStore { @@ -77,13 +76,6 @@ namespace MusicStore .AddSqlServer() .AddDbContext(); - // Add Identity services to the services container - services.AddIdentity(Configuration) - .AddEntityFrameworkStores() - .AddDefaultTokenProviders() - .AddMessageProvider() - .AddMessageProvider(); - // Add MVC services to the services container services.AddMvc(); @@ -119,7 +111,7 @@ namespace MusicStore }); //Populates the MusicStore sample data - SampleData.InitializeMusicStoreDatabaseAsync(app.ApplicationServices).Wait(); + SampleData.InitializeMusicStoreDatabaseAsync(app.ApplicationServices, false).Wait(); } } -} +} \ No newline at end of file