Updating MusicStore.Spa for EF and Identity changes
This commit is contained in:
parent
41e8d22d3a
commit
bc1994eebd
|
|
@ -4,9 +4,6 @@
|
||||||
"Data": {
|
"Data": {
|
||||||
"DefaultConnection": {
|
"DefaultConnection": {
|
||||||
"Connectionstring": "Server=(localdb)\\MSSQLLocalDB;Database=MusicStoreSpa;Trusted_Connection=True;"
|
"Connectionstring": "Server=(localdb)\\MSSQLLocalDB;Database=MusicStoreSpa;Trusted_Connection=True;"
|
||||||
},
|
|
||||||
"IdentityConnection": {
|
|
||||||
"Connectionstring": "Server=(localdb)\\MSSQLLocalDB;Database=MusicStoreSpaIdentity;Trusted_Connection=True;"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1,26 +0,0 @@
|
||||||
using System;
|
|
||||||
using Microsoft.AspNet.Identity;
|
|
||||||
using Microsoft.AspNet.Identity.EntityFramework;
|
|
||||||
using Microsoft.Data.Entity;
|
|
||||||
using Microsoft.Framework.OptionsModel;
|
|
||||||
|
|
||||||
namespace MusicStore.Models
|
|
||||||
{
|
|
||||||
public class ApplicationUser : IdentityUser { }
|
|
||||||
|
|
||||||
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
|
|
||||||
{
|
|
||||||
public ApplicationDbContext(IServiceProvider serviceProvider, IOptions<IdentityDbContextOptions> optionsAccessor)
|
|
||||||
: base(serviceProvider, optionsAccessor.Options)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public class IdentityDbContextOptions : DbContextOptions
|
|
||||||
{
|
|
||||||
public string DefaultAdminUserName { get; set; }
|
|
||||||
|
|
||||||
public string DefaultAdminPassword { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,14 +1,18 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using Microsoft.AspNet.Identity;
|
||||||
|
using Microsoft.AspNet.Identity.EntityFramework;
|
||||||
using Microsoft.Data.Entity;
|
using Microsoft.Data.Entity;
|
||||||
using Microsoft.Data.Entity.Metadata;
|
using Microsoft.Data.Entity.Metadata;
|
||||||
using Microsoft.Framework.OptionsModel;
|
using Microsoft.Framework.OptionsModel;
|
||||||
|
|
||||||
namespace MusicStore.Models
|
namespace MusicStore.Models
|
||||||
{
|
{
|
||||||
public class MusicStoreContext : DbContext
|
public class ApplicationUser : IdentityUser { }
|
||||||
|
|
||||||
|
public class MusicStoreContext : IdentityDbContext<ApplicationUser>
|
||||||
{
|
{
|
||||||
public MusicStoreContext(IServiceProvider serviceProvider, IOptions<MusicStoreDbContextOptions> optionsAccessor)
|
public MusicStoreContext(IServiceProvider serviceProvider, IOptions<DbContextOptions<MusicStoreContext>> optionsAccessor)
|
||||||
: base(serviceProvider, optionsAccessor.Options)
|
: base(serviceProvider, optionsAccessor.Options)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
@ -35,13 +39,13 @@ namespace MusicStore.Models
|
||||||
builder.Entity<Album>().Key(a => a.AlbumId);
|
builder.Entity<Album>().Key(a => a.AlbumId);
|
||||||
builder.Entity<Artist>().Key(a => a.ArtistId);
|
builder.Entity<Artist>().Key(a => a.ArtistId);
|
||||||
|
|
||||||
builder.Entity<Order>(
|
builder.Entity<Order>(b =>
|
||||||
b =>
|
{
|
||||||
{
|
b.Key(o => o.OrderId);
|
||||||
b.Key(o => o.OrderId);
|
b.Property(o => o.OrderId)
|
||||||
b.Property(o => o.OrderId)
|
.ForRelational()
|
||||||
.ForRelational().Column("[Order]");
|
.Column("[Order]");
|
||||||
});
|
});
|
||||||
|
|
||||||
builder.Entity<Genre>().Key(g => g.GenreId);
|
builder.Entity<Genre>().Key(g => g.GenreId);
|
||||||
builder.Entity<CartItem>().Key(ci => ci.CartItemId);
|
builder.Entity<CartItem>().Key(ci => ci.CartItemId);
|
||||||
|
|
@ -53,16 +57,16 @@ namespace MusicStore.Models
|
||||||
builder.Entity<Genre>().Property(g => g.GenreId).GenerateValuesOnAdd(generateValues: false);
|
builder.Entity<Genre>().Property(g => g.GenreId).GenerateValuesOnAdd(generateValues: false);
|
||||||
|
|
||||||
builder.Entity<Album>(b =>
|
builder.Entity<Album>(b =>
|
||||||
{
|
{
|
||||||
b.ForeignKey<Genre>(a => a.GenreId);
|
b.ForeignKey<Genre>(a => a.GenreId);
|
||||||
b.ForeignKey<Artist>(a => a.ArtistId);
|
b.ForeignKey<Artist>(a => a.ArtistId);
|
||||||
});
|
});
|
||||||
|
|
||||||
builder.Entity<OrderDetail>(b =>
|
builder.Entity<OrderDetail>(b =>
|
||||||
{
|
{
|
||||||
b.ForeignKey<Album>(a => a.AlbumId);
|
b.ForeignKey<Album>(a => a.AlbumId);
|
||||||
b.ForeignKey<Order>(a => a.OrderId);
|
b.ForeignKey<Order>(a => a.OrderId);
|
||||||
});
|
});
|
||||||
|
|
||||||
var genre = builder.Model.GetEntityType(typeof(Genre));
|
var genre = builder.Model.GetEntityType(typeof(Genre));
|
||||||
var album = builder.Model.GetEntityType(typeof(Album));
|
var album = builder.Model.GetEntityType(typeof(Album));
|
||||||
|
|
@ -72,11 +76,8 @@ namespace MusicStore.Models
|
||||||
album.AddNavigation("OrderDetails", orderDetail.ForeignKeys.Single(k => k.ReferencedEntityType == album), pointsToPrincipal: false);
|
album.AddNavigation("OrderDetails", orderDetail.ForeignKeys.Single(k => k.ReferencedEntityType == album), pointsToPrincipal: false);
|
||||||
album.AddNavigation("Genre", album.ForeignKeys.Single(k => k.ReferencedEntityType == genre), pointsToPrincipal: true);
|
album.AddNavigation("Genre", album.ForeignKeys.Single(k => k.ReferencedEntityType == genre), pointsToPrincipal: true);
|
||||||
album.AddNavigation("Artist", album.ForeignKeys.Single(k => k.ReferencedEntityType == artist), pointsToPrincipal: true);
|
album.AddNavigation("Artist", album.ForeignKeys.Single(k => k.ReferencedEntityType == artist), pointsToPrincipal: true);
|
||||||
|
|
||||||
|
base.OnModelCreating(builder);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class MusicStoreDbContextOptions : DbContextOptions
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
@ -6,8 +6,9 @@ using System.Threading.Tasks;
|
||||||
using Microsoft.AspNet.Identity;
|
using Microsoft.AspNet.Identity;
|
||||||
using Microsoft.Data.Entity;
|
using Microsoft.Data.Entity;
|
||||||
using Microsoft.Data.Entity.SqlServer;
|
using Microsoft.Data.Entity.SqlServer;
|
||||||
using Microsoft.Framework.OptionsModel;
|
|
||||||
using Microsoft.Framework.DependencyInjection;
|
using Microsoft.Framework.DependencyInjection;
|
||||||
|
using Microsoft.Framework.OptionsModel;
|
||||||
|
using MusicStore.Spa;
|
||||||
|
|
||||||
namespace MusicStore.Models
|
namespace MusicStore.Models
|
||||||
{
|
{
|
||||||
|
|
@ -25,29 +26,12 @@ namespace MusicStore.Models
|
||||||
if (await db.Database.EnsureCreatedAsync())
|
if (await db.Database.EnsureCreatedAsync())
|
||||||
{
|
{
|
||||||
await InsertTestData(serviceProvider);
|
await InsertTestData(serviceProvider);
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
await InsertTestData(serviceProvider);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static async Task InitializeIdentityDatabaseAsync(IServiceProvider serviceProvider)
|
|
||||||
{
|
|
||||||
using (var db = serviceProvider.GetService<ApplicationDbContext>())
|
|
||||||
{
|
|
||||||
var sqlServerDataStore = db.Configuration.DataStore as SqlServerDataStore;
|
|
||||||
if (sqlServerDataStore != null)
|
|
||||||
{
|
|
||||||
if (await db.Database.EnsureCreatedAsync())
|
|
||||||
{
|
|
||||||
await CreateAdminUser(serviceProvider);
|
await CreateAdminUser(serviceProvider);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
await InsertTestData(serviceProvider);
|
||||||
await CreateAdminUser(serviceProvider);
|
await CreateAdminUser(serviceProvider);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -55,7 +39,7 @@ namespace MusicStore.Models
|
||||||
|
|
||||||
private static async Task CreateAdminUser(IServiceProvider serviceProvider)
|
private static async Task CreateAdminUser(IServiceProvider serviceProvider)
|
||||||
{
|
{
|
||||||
var options = serviceProvider.GetService<IOptions<IdentityDbContextOptions>>().Options;
|
var settings = serviceProvider.GetService<IOptions<SiteSettings>>().Options;
|
||||||
const string adminRole = "Administrator";
|
const string adminRole = "Administrator";
|
||||||
|
|
||||||
var userManager = serviceProvider.GetService<UserManager<ApplicationUser>>();
|
var userManager = serviceProvider.GetService<UserManager<ApplicationUser>>();
|
||||||
|
|
@ -66,11 +50,11 @@ namespace MusicStore.Models
|
||||||
await roleManager.CreateAsync(new IdentityRole(adminRole));
|
await roleManager.CreateAsync(new IdentityRole(adminRole));
|
||||||
}
|
}
|
||||||
|
|
||||||
var user = await userManager.FindByNameAsync(options.DefaultAdminUserName);
|
var user = await userManager.FindByNameAsync(settings.DefaultAdminUsername);
|
||||||
if (user == null)
|
if (user == null)
|
||||||
{
|
{
|
||||||
user = new ApplicationUser { UserName = options.DefaultAdminUserName };
|
user = new ApplicationUser { UserName = settings.DefaultAdminUsername };
|
||||||
await userManager.CreateAsync(user, options.DefaultAdminPassword);
|
await userManager.CreateAsync(user, settings.DefaultAdminPassword);
|
||||||
await userManager.AddToRoleAsync(user, adminRole);
|
await userManager.AddToRoleAsync(user, adminRole);
|
||||||
await userManager.AddClaimAsync(user, new Claim("ManageStore", "Allowed"));
|
await userManager.AddClaimAsync(user, new Claim("ManageStore", "Allowed"));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,10 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace MusicStore.Spa
|
||||||
|
{
|
||||||
|
public class SiteSettings
|
||||||
|
{
|
||||||
|
public string DefaultAdminUsername { get; set; }
|
||||||
|
public string DefaultAdminPassword { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -27,29 +27,27 @@ namespace MusicStore.Spa
|
||||||
|
|
||||||
public void ConfigureServices(IServiceCollection services)
|
public void ConfigureServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
// Add options accessors to the service container
|
services.Configure<SiteSettings>(settings =>
|
||||||
services.Configure<IdentityDbContextOptions>(options =>
|
|
||||||
{
|
{
|
||||||
options.DefaultAdminUserName = Configuration.Get("DefaultAdminUsername");
|
settings.DefaultAdminUsername = Configuration.Get("DefaultAdminUsername");
|
||||||
options.DefaultAdminPassword = Configuration.Get("DefaultAdminPassword");
|
settings.DefaultAdminPassword = Configuration.Get("DefaultAdminPassword");
|
||||||
options.UseSqlServer(Configuration.Get("Data:IdentityConnection:ConnectionString"));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
services.Configure<MusicStoreDbContextOptions>(options =>
|
|
||||||
options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString")));
|
|
||||||
|
|
||||||
// Add MVC services to the service container
|
// Add MVC services to the service container
|
||||||
services.AddMvc();
|
services.AddMvc();
|
||||||
|
|
||||||
// Add EF services to the service container
|
// Add EF services to the service container
|
||||||
services.AddEntityFramework()
|
services.AddEntityFramework()
|
||||||
.AddSqlServer();
|
.AddSqlServer()
|
||||||
|
.AddDbContext<MusicStoreContext>(options =>
|
||||||
|
{
|
||||||
|
options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString"));
|
||||||
|
});
|
||||||
|
|
||||||
// Add Identity services to the services container
|
// Add Identity services to the services container
|
||||||
services.AddDefaultIdentity<ApplicationDbContext, ApplicationUser, IdentityRole>(Configuration);
|
services.AddDefaultIdentity<MusicStoreContext, ApplicationUser, IdentityRole>(Configuration);
|
||||||
|
|
||||||
// Add application services to the service container
|
// Add application services to the service container
|
||||||
services.AddScoped<MusicStoreContext>();
|
|
||||||
services.AddTransient(typeof(IHtmlHelper<>), typeof(AngularHtmlHelper<>));
|
services.AddTransient(typeof(IHtmlHelper<>), typeof(AngularHtmlHelper<>));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -57,7 +55,6 @@ namespace MusicStore.Spa
|
||||||
{
|
{
|
||||||
// Initialize the sample data
|
// Initialize the sample data
|
||||||
SampleData.InitializeMusicStoreDatabaseAsync(app.ApplicationServices).Wait();
|
SampleData.InitializeMusicStoreDatabaseAsync(app.ApplicationServices).Wait();
|
||||||
SampleData.InitializeIdentityDatabaseAsync(app.ApplicationServices).Wait();
|
|
||||||
|
|
||||||
// Configure the HTTP request pipeline
|
// Configure the HTTP request pipeline
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue