public static async Task <ChessGamesDbContext> InitDbAsync(string[] args = null, ILoggerFactory loggerFactory = null) { var config = ConfigurationExtensions.Configuration(args); var dbType = config.ServerType(); var dbContext = config.CreateDbContext(dbType, loggerFactory: loggerFactory); Reporter($"Connecting to {dbType} chess database..."); Reporter(" Checking for pending migrations..."); var pendingMigrations = (await RelationalDatabaseFacadeExtensions.GetPendingMigrationsAsync(dbContext.Database)).ToList(); if (pendingMigrations.Any()) { Reporter($" Applying {pendingMigrations.Count} pending migration(s)..."); await dbContext.Database.MigrateAsync(); Reporter(" ... database successfully updated."); } else { Reporter(" No pending migrations"); } return(dbContext); }
private static void Startup(string[] args) { DbStartup.Reporter = Reporter; Configuration = ConfigurationExtensions.Configuration(args); Log.Logger = new LoggerConfiguration() .ReadFrom .Configuration(Configuration) .CreateLogger(); var loggerFactory = new SerilogLoggerFactory(Log.Logger); _dbContext = DbStartup.InitDbAsync(args, loggerFactory).Result; _mapper = AutoMapperFactory.Create(); var pgnRepository = new PgnRepository(_dbContext, loggerFactory.CreateLogger <PgnRepository>()); _svc = new PgnImportService(pgnRepository, _mapper, loggerFactory.CreateLogger <PgnImportService>()); _svc.Status += ShowStatus; }
public static async Task Main(string[] args) { Log.Logger = new LoggerConfiguration() .ReadFrom .Configuration(ConfigurationExtensions.Configuration()) .CreateLogger(); var host = CreateHostBuilder(args) .Build(); try { Log.Information("Migrating database to latest version."); DbStartup.Reporter = Log.Information; await DbStartup.InitDbAsync(args); } catch (Exception e) { Log.Fatal(e, "An error occured while migrating the database."); throw; } try { Log.Information("Starting web host"); host.Run(); } catch (Exception ex) { Log.Fatal(ex, "Web host terminated unexpectedly"); } finally { Log.Information("Shutting down."); Log.CloseAndFlush(); } }