Using .Include in queries instead of manual joining
Also removed Identity setup from Ntlm Startup Some minor using statements ordering.
This commit is contained in:
parent
681327f396
commit
e21f6052f2
|
|
@ -1,9 +1,9 @@
|
||||||
using Microsoft.Framework.Cache.Memory;
|
using System;
|
||||||
using Microsoft.AspNet.Mvc;
|
|
||||||
using MusicStore.Models;
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Microsoft.Framework.Cache.Memory;
|
||||||
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using MusicStore.Models;
|
||||||
|
|
||||||
namespace MusicStore.Controllers
|
namespace MusicStore.Controllers
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
using Microsoft.Framework.Cache.Memory;
|
using System;
|
||||||
using Microsoft.AspNet.Mvc;
|
|
||||||
using MusicStore.Models;
|
|
||||||
using System;
|
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Microsoft.AspNet.Mvc;
|
||||||
|
using Microsoft.Framework.Cache.Memory;
|
||||||
|
using MusicStore.Models;
|
||||||
|
|
||||||
namespace MusicStore.Controllers
|
namespace MusicStore.Controllers
|
||||||
{
|
{
|
||||||
|
|
@ -33,11 +33,7 @@ namespace MusicStore.Controllers
|
||||||
public IActionResult Browse(string genre)
|
public IActionResult Browse(string genre)
|
||||||
{
|
{
|
||||||
// Retrieve Genre genre and its Associated associated Albums albums from database
|
// Retrieve Genre genre and its Associated associated Albums albums from database
|
||||||
|
var genreModel = db.Genres.Include(g => g.Albums).Where(g => g.Name == genre).FirstOrDefault();
|
||||||
// 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();
|
|
||||||
|
|
||||||
return View(genreModel);
|
return View(genreModel);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -48,11 +44,7 @@ namespace MusicStore.Controllers
|
||||||
//Remove it from cache if not retrieved in last 10 minutes
|
//Remove it from cache if not retrieved in last 10 minutes
|
||||||
context.SetSlidingExpiration(TimeSpan.FromMinutes(10));
|
context.SetSlidingExpiration(TimeSpan.FromMinutes(10));
|
||||||
|
|
||||||
var albumData = db.Albums.Single(a => a.AlbumId == id);
|
var albumData = db.Albums.Where(a => a.AlbumId == id).Include(a => a.Artist).Include(a => a.Genre).ToList().FirstOrDefault();
|
||||||
|
|
||||||
// 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);
|
|
||||||
return albumData;
|
return albumData;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,11 +9,6 @@ namespace MusicStore.Models
|
||||||
|
|
||||||
public class MusicStoreContext : IdentityDbContext<ApplicationUser>
|
public class MusicStoreContext : IdentityDbContext<ApplicationUser>
|
||||||
{
|
{
|
||||||
public MusicStoreContext()
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public DbSet<Album> Albums { get; set; }
|
public DbSet<Album> Albums { get; set; }
|
||||||
public DbSet<Artist> Artists { get; set; }
|
public DbSet<Artist> Artists { get; set; }
|
||||||
public DbSet<Order> Orders { get; set; }
|
public DbSet<Order> Orders { get; set; }
|
||||||
|
|
@ -34,8 +29,13 @@ namespace MusicStore.Models
|
||||||
builder.Entity<Artist>().Property(a => a.ArtistId).GenerateValueOnAdd(generateValue: false);
|
builder.Entity<Artist>().Property(a => a.ArtistId).GenerateValueOnAdd(generateValue: false);
|
||||||
builder.Entity<Genre>().Property(g => g.GenreId).GenerateValueOnAdd(generateValue: false);
|
builder.Entity<Genre>().Property(g => g.GenreId).GenerateValueOnAdd(generateValue: false);
|
||||||
|
|
||||||
|
// TODO: Remove this once convention-based relations work again
|
||||||
|
builder.Entity<Album>().ManyToOne(a => a.Artist);
|
||||||
|
builder.Entity<Album>().ManyToOne(a => a.Genre, g => g.Albums);
|
||||||
|
builder.Entity<Order>().OneToMany(o => o.OrderDetails);
|
||||||
|
builder.Entity<Album>().OneToMany(a => a.OrderDetails, od => od.Album);
|
||||||
|
|
||||||
base.OnModelCreating(builder);
|
base.OnModelCreating(builder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -17,7 +17,7 @@ namespace MusicStore.Models
|
||||||
const string defaultAdminUserName = "DefaultAdminUserName";
|
const string defaultAdminUserName = "DefaultAdminUserName";
|
||||||
const string defaultAdminPassword = "defaultAdminPassword";
|
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<MusicStoreContext>())
|
using (var db = serviceProvider.GetService<MusicStoreContext>())
|
||||||
{
|
{
|
||||||
|
|
@ -27,13 +27,19 @@ namespace MusicStore.Models
|
||||||
if (await sqlServerDatabase.EnsureCreatedAsync())
|
if (await sqlServerDatabase.EnsureCreatedAsync())
|
||||||
{
|
{
|
||||||
await InsertTestData(serviceProvider);
|
await InsertTestData(serviceProvider);
|
||||||
await CreateAdminUser(serviceProvider);
|
if (createUsers)
|
||||||
|
{
|
||||||
|
await CreateAdminUser(serviceProvider);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
await InsertTestData(serviceProvider);
|
await InsertTestData(serviceProvider);
|
||||||
await CreateAdminUser(serviceProvider);
|
if (createUsers)
|
||||||
|
{
|
||||||
|
await CreateAdminUser(serviceProvider);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ using Microsoft.AspNet.Server.WebListener;
|
||||||
using System.Security.Claims;
|
using System.Security.Claims;
|
||||||
using System.Security.Principal;
|
using System.Security.Principal;
|
||||||
using Microsoft.Framework.Cache.Memory;
|
using Microsoft.Framework.Cache.Memory;
|
||||||
using Microsoft.AspNet.Identity;
|
|
||||||
|
|
||||||
namespace MusicStore
|
namespace MusicStore
|
||||||
{
|
{
|
||||||
|
|
@ -77,13 +76,6 @@ namespace MusicStore
|
||||||
.AddSqlServer()
|
.AddSqlServer()
|
||||||
.AddDbContext<MusicStoreContext>();
|
.AddDbContext<MusicStoreContext>();
|
||||||
|
|
||||||
// Add Identity services to the services container
|
|
||||||
services.AddIdentity<ApplicationUser, IdentityRole>(Configuration)
|
|
||||||
.AddEntityFrameworkStores<MusicStoreContext>()
|
|
||||||
.AddDefaultTokenProviders()
|
|
||||||
.AddMessageProvider<EmailMessageProvider>()
|
|
||||||
.AddMessageProvider<SmsMessageProvider>();
|
|
||||||
|
|
||||||
// Add MVC services to the services container
|
// Add MVC services to the services container
|
||||||
services.AddMvc();
|
services.AddMvc();
|
||||||
|
|
||||||
|
|
@ -119,7 +111,7 @@ namespace MusicStore
|
||||||
});
|
});
|
||||||
|
|
||||||
//Populates the MusicStore sample data
|
//Populates the MusicStore sample data
|
||||||
SampleData.InitializeMusicStoreDatabaseAsync(app.ApplicationServices).Wait();
|
SampleData.InitializeMusicStoreDatabaseAsync(app.ApplicationServices, false).Wait();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue