public async static Task Main(string[] args) { var logger = NLog.Web.NLogBuilder.ConfigureNLog("nlog.config").GetCurrentClassLogger(); logger.Info(Directory.GetCurrentDirectory()); var host = CreateHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { var userManager = services.GetRequiredService <UserManager <ApplicationUser> >(); var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >(); var northwindContext = services.GetRequiredService <NorthwindDbContext>(); if (northwindContext.Database.IsSqlServer() && !await northwindContext.Database.CanConnectAsync()) { await northwindContext.Database.MigrateAsync(); var seeder = new NorthwindDbContextSeeder(northwindContext, userManager, roleManager); await seeder.SeedAllAsync(CancellationToken.None); } } catch (Exception ex) { logger.Error(ex, "An error occurred while migrating or initializing the database."); throw; } } await host.RunAsync(); }
public async static Task Main(string[] args) { var host = CreateHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { var userManager = services.GetRequiredService <UserManager <ApplicationUser> >(); var northwindContext = services.GetRequiredService <NorthwindDbContext>(); if (northwindContext.Database.IsSqlServer() && !await northwindContext.Database.CanConnectAsync()) { await northwindContext.Database.MigrateAsync(); var seeder = new NorthwindDbContextSeeder(northwindContext, userManager); await seeder.SeedAllAsync(CancellationToken.None); } } catch (Exception ex) { var logger = scope.ServiceProvider.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred while migrating or initializing the database."); throw; } } await host.RunAsync(); }