Merging the MusicStore & MusicStoreIdentity Db into one.

This has been a feedback from identity team for a while. Just got time to do it now.
This commit is contained in:
Praburaj 2014-07-22 16:44:23 -07:00
parent 53e4626127
commit 3c0eefe12e
7 changed files with 17 additions and 65 deletions

View File

@ -4,9 +4,6 @@
"Data": {
"DefaultConnection": {
"Connectionstring": "Server=(localdb)\\MSSQLLocalDB;Database=MusicStore;Trusted_Connection=True;MultipleActiveResultSets=true"
},
"IdentityConnection": {
"Connectionstring": "Server=(localdb)\\MSSQLLocalDB;Database=MusicStoreIdentity;Trusted_Connection=True;MultipleActiveResultSets=true"
}
}
}

View File

@ -1,25 +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, IOptionsAccessor<IdentityDbContextOptions> optionsAccessor)
: base(serviceProvider, optionsAccessor.Options)
{
}
}
public class IdentityDbContextOptions : DbContextOptions
{
public string DefaultAdminUserName { get; set; }
public string DefaultAdminPassword { get; set; }
}
}

View File

@ -1,11 +1,15 @@
using System;
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<ApplicationUser>
{
public MusicStoreContext(IServiceProvider serviceProvider, IOptionsAccessor<MusicStoreDbContextOptions> optionsAccessor)
: base(serviceProvider, optionsAccessor.Options)
@ -20,20 +24,23 @@ namespace MusicStore.Models
public DbSet<CartItem> CartItems { get; set; }
public DbSet<OrderDetail> OrderDetails { get; set; }
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Album>().Key(a => a.AlbumId);
builder.Entity<Artist>().Key(a => a.ArtistId);
builder.Entity<Order>().Key(o => o.OrderId).Properties(p => p.Property(o => o.OrderId).ColumnName("Order"));
builder.Entity<Order>().Key(o => o.OrderId);
builder.Entity<Genre>().Key(g => g.GenreId);
builder.Entity<CartItem>().Key(c => c.CartItemId);
builder.Entity<OrderDetail>().Key(o => o.OrderDetailId);
base.OnModelCreating(builder);
}
}
public class MusicStoreDbContextOptions : DbContextOptions
{
public string DefaultAdminUserName { get; set; }
public string DefaultAdminPassword { get; set; }
}
}

View File

@ -25,29 +25,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<ApplicationDbContext>())
{
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);
}
}
@ -88,7 +71,6 @@ namespace MusicStore.Models
}
}
/// <summary>
/// Creates a store manager user who can manage the inventory.
/// </summary>
@ -96,7 +78,7 @@ namespace MusicStore.Models
/// <returns></returns>
private static async Task CreateAdminUser(IServiceProvider serviceProvider)
{
var options = serviceProvider.GetService<IOptionsAccessor<IdentityDbContextOptions>>().Options;
var options = serviceProvider.GetService<IOptionsAccessor<MusicStoreDbContextOptions>>().Options;
//const string adminRole = "Administrator";
var userManager = serviceProvider.GetService<UserManager<ApplicationUser>>();

View File

@ -91,7 +91,6 @@
<Compile Include="Models\Artist.cs" />
<Compile Include="Models\CartItem.cs" />
<Compile Include="Models\Genre.cs" />
<Compile Include="Models\IdentityModels.cs" />
<Compile Include="Models\MusicStoreContext.cs" />
<Compile Include="Models\Order.cs" />
<Compile Include="Models\OrderDetail.cs" />
@ -103,4 +102,4 @@
<Compile Include="ViewModels\ShoppingCartViewModel.cs" />
</ItemGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>
</Project>

View File

@ -33,18 +33,15 @@ namespace MusicStore
services.AddScoped<MusicStoreContext>();
// Configure DbContext
services.SetupOptions<IdentityDbContextOptions>(options =>
services.SetupOptions<MusicStoreDbContextOptions>(options =>
{
options.DefaultAdminUserName = configuration.Get("DefaultAdminUsername");
options.DefaultAdminPassword = configuration.Get("DefaultAdminPassword");
options.UseSqlServer(configuration.Get("Data:IdentityConnection:ConnectionString"));
options.UseSqlServer(configuration.Get("Data:DefaultConnection:ConnectionString"));
});
services.SetupOptions<MusicStoreDbContextOptions>(options =>
options.UseSqlServer(configuration.Get("Data:DefaultConnection:ConnectionString")));
// Add Identity services to the services container
services.AddIdentitySqlServer<ApplicationDbContext, ApplicationUser>()
services.AddIdentitySqlServer<MusicStoreContext, ApplicationUser>()
.AddHttpSignIn();
// Add MVC services to the services container
@ -81,7 +78,6 @@ namespace MusicStore
//Populates the MusicStore sample data
SampleData.InitializeMusicStoreDatabaseAsync(app.ApplicationServices).Wait();
SampleData.InitializeIdentityDatabaseAsync(app.ApplicationServices).Wait();
}
}
}

View File

@ -28,14 +28,11 @@ namespace E2ETests
var testStartTime = DateTime.Now;
var musicStoreDbName = Guid.NewGuid().ToString().Replace("-", string.Empty);
var musicStoreIdentityDbName = Guid.NewGuid().ToString().Replace("-", string.Empty);
Console.WriteLine("Pointing MusicStore DB to '{0}'", string.Format(Connection_string_Format, musicStoreDbName));
Console.WriteLine("Pointing MusicStoreIdentity DB to '{0}'", string.Format(Connection_string_Format, musicStoreIdentityDbName));
//Override the connection strings using environment based configuration
Environment.SetEnvironmentVariable("SQLAZURECONNSTR_DefaultConnection", string.Format(Connection_string_Format, musicStoreDbName));
Environment.SetEnvironmentVariable("SQLAZURECONNSTR_IdentityConnection", string.Format(Connection_string_Format, musicStoreIdentityDbName));
ApplicationBaseUrl = applicationBaseUrl;
Process hostProcess = null;
@ -43,7 +40,7 @@ namespace E2ETests
try
{
hostProcess = DeploymentUtility.StartApplication(hostType, kreFlavor, musicStoreIdentityDbName);
hostProcess = DeploymentUtility.StartApplication(hostType, kreFlavor, musicStoreDbName);
httpClientHandler = new HttpClientHandler();
httpClient = new HttpClient(httpClientHandler) { BaseAddress = new Uri(applicationBaseUrl) };
@ -147,7 +144,6 @@ namespace E2ETests
}
DbUtils.DropDatabase(musicStoreDbName);
DbUtils.DropDatabase(musicStoreIdentityDbName);
}
}