Cleaning up EF 2.0 Migrations (#57)

Moving Seed logic to Program.cs
This commit is contained in:
Steve Smith
2017-10-18 14:25:10 -04:00
committed by GitHub
parent 8de663eab6
commit 32950aa175
9 changed files with 456 additions and 221 deletions

View File

@@ -1,14 +1,46 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore;
using Microsoft.Extensions.DependencyInjection;
using Infrastructure.Data;
using System;
using Microsoft.Extensions.Logging;
using Infrastructure.Identity;
using Microsoft.AspNetCore.Identity;
namespace Microsoft.eShopWeb
{
public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
var host = BuildWebHost(args);
using (var scope = host.Services.CreateScope())
{
var services = scope.ServiceProvider;
try
{
var catalogContext = services.GetRequiredService<CatalogContext>();
var loggerFactory = services.GetRequiredService<ILoggerFactory>();
CatalogContextSeed.SeedAsync(catalogContext, loggerFactory)
.Wait();
// move to IdentitySeed method
var userManager = services.GetRequiredService<UserManager<ApplicationUser>>();
var defaultUser = new ApplicationUser { UserName = "demouser@microsoft.com", Email = "demouser@microsoft.com" };
userManager.CreateAsync(defaultUser, "Pass@word1").Wait();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
//var logger = services.GetRequiredService<ILogger<Program>>();
//logger.LogError(ex, "An error occurred seeding the DB.");
}
}
host.Run();
}
public static IWebHost BuildWebHost(string[] args) =>

View File

@@ -32,14 +32,15 @@ namespace Microsoft.eShopWeb
public void ConfigureServices(IServiceCollection services)
{
// Requires LocalDB which can be installed with SQL Server Express 2016
// https://www.microsoft.com/en-us/download/details.aspx?id=54284
services.AddDbContext<CatalogContext>(c =>
{
try
{
c.UseInMemoryDatabase("Catalog");
//c.UseSqlServer(Configuration.GetConnectionString("CatalogConnection"));
//c.UseInMemoryDatabase("Catalog");
// Requires LocalDB which can be installed with SQL Server Express 2016
// https://www.microsoft.com/en-us/download/details.aspx?id=54284
c.UseSqlServer(Configuration.GetConnectionString("CatalogConnection"));
}
catch (System.Exception ex )
{
@@ -49,8 +50,8 @@ namespace Microsoft.eShopWeb
// Add Identity DbContext
services.AddDbContext<AppIdentityDbContext>(options =>
options.UseInMemoryDatabase("Identity"));
//options.UseSqlServer(Configuration.GetConnectionString("IdentityConnection")));
//options.UseInMemoryDatabase("Identity"));
options.UseSqlServer(Configuration.GetConnectionString("IdentityConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<AppIdentityDbContext>()
@@ -129,44 +130,21 @@ namespace Microsoft.eShopWeb
}));
}
public void ConfigureDevelopment(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory,
UserManager<ApplicationUser> userManager,
CatalogContext catalogContext)
{
Configure(app, env);
// moved to Program.cs
//public void ConfigureDevelopment(IApplicationBuilder app,
// IHostingEnvironment env,
// ILoggerFactory loggerFactory,
// UserManager<ApplicationUser> userManager,
// CatalogContext catalogContext)
//{
// Configure(app, env);
//Seed Data
CatalogContextSeed.SeedAsync(app, catalogContext, loggerFactory)
.Wait();
// //Seed Data
// CatalogContextSeed.SeedAsync(app, catalogContext, loggerFactory)
// .Wait();
var defaultUser = new ApplicationUser { UserName = "demouser@microsoft.com", Email = "demouser@microsoft.com" };
userManager.CreateAsync(defaultUser, "Pass@word1").Wait();
}
/// <summary>
/// Use this section to perform production-specific configuration.
/// In this case it is duplicating Development so that deployments to Azure will have sample data immediately.
/// </summary>
/// <param name="app"></param>
/// <param name="env"></param>
/// <param name="loggerFactory"></param>
/// <param name="userManager"></param>
public void ConfigureProduction(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory,
UserManager<ApplicationUser> userManager,
CatalogContext catalogContext)
{
Configure(app, env);
//Seed Data
CatalogContextSeed.SeedAsync(app, catalogContext, loggerFactory)
.Wait();
var defaultUser = new ApplicationUser { UserName = "demouser@microsoft.com", Email = "demouser@microsoft.com" };
userManager.CreateAsync(defaultUser, "Pass@word1").Wait();
}
// var defaultUser = new ApplicationUser { UserName = "demouser@microsoft.com", Email = "demouser@microsoft.com" };
// userManager.CreateAsync(defaultUser, "Pass@word1").Wait();
//}
}
}

View File

@@ -23,6 +23,7 @@
<ProjectReference Include="..\Infrastructure\Infrastructure.csproj" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.Extensions.SecretManager.Tools" Version="2.0.0" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="2.0.0" />