diff --git a/src/MusicStore.Spa/Config.json b/src/MusicStore.Spa/Config.json index f40b4813d6..f8e16de469 100644 --- a/src/MusicStore.Spa/Config.json +++ b/src/MusicStore.Spa/Config.json @@ -4,9 +4,6 @@ "Data": { "DefaultConnection": { "Connectionstring": "Server=(localdb)\\MSSQLLocalDB;Database=MusicStoreSpa;Trusted_Connection=True;" - }, - "IdentityConnection": { - "Connectionstring": "Server=(localdb)\\MSSQLLocalDB;Database=MusicStoreSpaIdentity;Trusted_Connection=True;" } } } \ No newline at end of file diff --git a/src/MusicStore.Spa/Models/IdentityModels.cs b/src/MusicStore.Spa/Models/IdentityModels.cs deleted file mode 100644 index 7ccfba846c..0000000000 --- a/src/MusicStore.Spa/Models/IdentityModels.cs +++ /dev/null @@ -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 - { - public ApplicationDbContext(IServiceProvider serviceProvider, IOptions optionsAccessor) - : base(serviceProvider, optionsAccessor.Options) - { - - } - } - - public class IdentityDbContextOptions : DbContextOptions - { - public string DefaultAdminUserName { get; set; } - - public string DefaultAdminPassword { get; set; } - } -} \ No newline at end of file diff --git a/src/MusicStore.Spa/Models/MusicStoreContext.cs b/src/MusicStore.Spa/Models/MusicStoreContext.cs index b7704e113e..e695a62072 100644 --- a/src/MusicStore.Spa/Models/MusicStoreContext.cs +++ b/src/MusicStore.Spa/Models/MusicStoreContext.cs @@ -1,14 +1,18 @@ using System; using System.Linq; +using Microsoft.AspNet.Identity; +using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.Data.Entity; using Microsoft.Data.Entity.Metadata; using Microsoft.Framework.OptionsModel; namespace MusicStore.Models { - public class MusicStoreContext : DbContext + public class ApplicationUser : IdentityUser { } + + public class MusicStoreContext : IdentityDbContext { - public MusicStoreContext(IServiceProvider serviceProvider, IOptions optionsAccessor) + public MusicStoreContext(IServiceProvider serviceProvider, IOptions> optionsAccessor) : base(serviceProvider, optionsAccessor.Options) { @@ -35,13 +39,13 @@ namespace MusicStore.Models builder.Entity().Key(a => a.AlbumId); builder.Entity().Key(a => a.ArtistId); - builder.Entity( - b => - { - b.Key(o => o.OrderId); - b.Property(o => o.OrderId) - .ForRelational().Column("[Order]"); - }); + builder.Entity(b => + { + b.Key(o => o.OrderId); + b.Property(o => o.OrderId) + .ForRelational() + .Column("[Order]"); + }); builder.Entity().Key(g => g.GenreId); builder.Entity().Key(ci => ci.CartItemId); @@ -53,16 +57,16 @@ namespace MusicStore.Models builder.Entity().Property(g => g.GenreId).GenerateValuesOnAdd(generateValues: false); builder.Entity(b => - { - b.ForeignKey(a => a.GenreId); - b.ForeignKey(a => a.ArtistId); - }); + { + b.ForeignKey(a => a.GenreId); + b.ForeignKey(a => a.ArtistId); + }); builder.Entity(b => - { - b.ForeignKey(a => a.AlbumId); - b.ForeignKey(a => a.OrderId); - }); + { + b.ForeignKey(a => a.AlbumId); + b.ForeignKey(a => a.OrderId); + }); var genre = builder.Model.GetEntityType(typeof(Genre)); 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("Genre", album.ForeignKeys.Single(k => k.ReferencedEntityType == genre), pointsToPrincipal: true); album.AddNavigation("Artist", album.ForeignKeys.Single(k => k.ReferencedEntityType == artist), pointsToPrincipal: true); + + base.OnModelCreating(builder); } } - - public class MusicStoreDbContextOptions : DbContextOptions - { - - } } \ No newline at end of file diff --git a/src/MusicStore.Spa/Models/SampleData.cs b/src/MusicStore.Spa/Models/SampleData.cs index 6d87b9ad67..bb6e4cd947 100644 --- a/src/MusicStore.Spa/Models/SampleData.cs +++ b/src/MusicStore.Spa/Models/SampleData.cs @@ -6,8 +6,9 @@ using System.Threading.Tasks; using Microsoft.AspNet.Identity; using Microsoft.Data.Entity; using Microsoft.Data.Entity.SqlServer; -using Microsoft.Framework.OptionsModel; using Microsoft.Framework.DependencyInjection; +using Microsoft.Framework.OptionsModel; +using MusicStore.Spa; namespace MusicStore.Models { @@ -25,29 +26,12 @@ namespace MusicStore.Models if (await db.Database.EnsureCreatedAsync()) { await InsertTestData(serviceProvider); - } - } - else - { - await InsertTestData(serviceProvider); - } - } - } - - public static async Task InitializeIdentityDatabaseAsync(IServiceProvider serviceProvider) - { - using (var db = serviceProvider.GetService()) - { - var sqlServerDataStore = db.Configuration.DataStore as SqlServerDataStore; - if (sqlServerDataStore != null) - { - if (await db.Database.EnsureCreatedAsync()) - { await CreateAdminUser(serviceProvider); } } else { + await InsertTestData(serviceProvider); await CreateAdminUser(serviceProvider); } } @@ -55,7 +39,7 @@ namespace MusicStore.Models private static async Task CreateAdminUser(IServiceProvider serviceProvider) { - var options = serviceProvider.GetService>().Options; + var settings = serviceProvider.GetService>().Options; const string adminRole = "Administrator"; var userManager = serviceProvider.GetService>(); @@ -66,11 +50,11 @@ namespace MusicStore.Models await roleManager.CreateAsync(new IdentityRole(adminRole)); } - var user = await userManager.FindByNameAsync(options.DefaultAdminUserName); + var user = await userManager.FindByNameAsync(settings.DefaultAdminUsername); if (user == null) { - user = new ApplicationUser { UserName = options.DefaultAdminUserName }; - await userManager.CreateAsync(user, options.DefaultAdminPassword); + user = new ApplicationUser { UserName = settings.DefaultAdminUsername }; + await userManager.CreateAsync(user, settings.DefaultAdminPassword); await userManager.AddToRoleAsync(user, adminRole); await userManager.AddClaimAsync(user, new Claim("ManageStore", "Allowed")); } diff --git a/src/MusicStore.Spa/SiteSettings.cs b/src/MusicStore.Spa/SiteSettings.cs new file mode 100644 index 0000000000..bf9d3ad286 --- /dev/null +++ b/src/MusicStore.Spa/SiteSettings.cs @@ -0,0 +1,10 @@ +using System; + +namespace MusicStore.Spa +{ + public class SiteSettings + { + public string DefaultAdminUsername { get; set; } + public string DefaultAdminPassword { get; set; } + } +} \ No newline at end of file diff --git a/src/MusicStore.Spa/Startup.cs b/src/MusicStore.Spa/Startup.cs index f628f6bd64..b73074262a 100644 --- a/src/MusicStore.Spa/Startup.cs +++ b/src/MusicStore.Spa/Startup.cs @@ -27,29 +27,27 @@ namespace MusicStore.Spa public void ConfigureServices(IServiceCollection services) { - // Add options accessors to the service container - services.Configure(options => + services.Configure(settings => { - options.DefaultAdminUserName = Configuration.Get("DefaultAdminUsername"); - options.DefaultAdminPassword = Configuration.Get("DefaultAdminPassword"); - options.UseSqlServer(Configuration.Get("Data:IdentityConnection:ConnectionString")); + settings.DefaultAdminUsername = Configuration.Get("DefaultAdminUsername"); + settings.DefaultAdminPassword = Configuration.Get("DefaultAdminPassword"); }); - services.Configure(options => - options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString"))); - // Add MVC services to the service container services.AddMvc(); // Add EF services to the service container services.AddEntityFramework() - .AddSqlServer(); + .AddSqlServer() + .AddDbContext(options => + { + options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString")); + }); // Add Identity services to the services container - services.AddDefaultIdentity(Configuration); + services.AddDefaultIdentity(Configuration); // Add application services to the service container - services.AddScoped(); services.AddTransient(typeof(IHtmlHelper<>), typeof(AngularHtmlHelper<>)); } @@ -57,7 +55,6 @@ namespace MusicStore.Spa { // Initialize the sample data SampleData.InitializeMusicStoreDatabaseAsync(app.ApplicationServices).Wait(); - SampleData.InitializeIdentityDatabaseAsync(app.ApplicationServices).Wait(); // Configure the HTTP request pipeline