public static void Main(string[] args) { try { var configuration = GetConfiguration(); Log.Logger = CreateSerilogLogger(configuration); Log.Information("Configuring web host ({ApplicationContext})...", AppName); IHost host = CreateHostBuilder(args, configuration).Build(); Log.Information("Applying migrations ({ApplicationContext})...", AppName); var lifetimeScope = host.Services.GetAutofacRoot(); host.MigrateDbContext <PersistedGrantDbContext>(lifetimeScope, (__, ___) => { }) .MigrateDbContext <ApplicationDbContext>(lifetimeScope, (context, container) => { var env = lifetimeScope.Resolve <IWebHostEnvironment>(); var settings = lifetimeScope.Resolve <IOptions <ApplicationSettings> >(); var logger = lifetimeScope.Resolve <ILogger <ApplicationDbContextSeed> >(); var userManager = lifetimeScope.Resolve <UserManager <ApplicationUser> >(); var roleManager = lifetimeScope.Resolve <RoleManager <ApplicationRole> >(); new ApplicationDbContextSeed() .SeedAsync(context, env, logger, roleManager, userManager) .Wait(); }) .MigrateDbContext <ConfigurationDbContext>(lifetimeScope, (context, services) => { ConfigurationDbContextSeed .SeedAsync(context, configuration) .Wait(); }); Log.Information("Starting web host ({ApplicationContext})...", AppName); host.Run(); } catch (Exception ex) { Log.Fatal(ex, "Program terminated unexpectedly ({ApplicationContext})!", AppName); //return 1; } finally { Log.CloseAndFlush(); } }
private void InitializeDatabase(IApplicationBuilder app, IHostingEnvironment env) { using (var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope()) { serviceScope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>().Database.Migrate(); var context = serviceScope.ServiceProvider.GetRequiredService <ConfigurationDbContext>(); ConfigurationDbContextSeed configurationDbContextSeed = new ConfigurationDbContextSeed(); configurationDbContextSeed.SeedAsync(context, Configuration).Wait(); var context2 = serviceScope.ServiceProvider.GetRequiredService <ApplicationDbContext>(); ApplicationDbContextSeed applicationDbContextSeed = new ApplicationDbContextSeed(); var settings = serviceScope.ServiceProvider.GetRequiredService <IOptions <AppSettings> >(); var logger = serviceScope.ServiceProvider.GetRequiredService <ILogger <ApplicationDbContextSeed> >(); applicationDbContextSeed.SeedAsync(context2, env, logger, settings).Wait(); /*context.Database.Migrate(); * if (!context.Clients.Any()) * { * foreach (var client in Config.GetClients()) * { * context.Clients.Add(client.ToEntity()); * } * context.SaveChanges(); * } * * if (!context.IdentityResources.Any()) * { * foreach (var resource in Config.GetIdentityResources()) * { * context.IdentityResources.Add(resource.ToEntity()); * } * context.SaveChanges(); * } * * if (!context.ApiResources.Any()) * { * foreach (var resource in Config.GetApis()) * { * context.ApiResources.Add(resource.ToEntity()); * } * context.SaveChanges(); * }*/ } }
private void InitializeDatabase(IApplicationBuilder app) { using var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope(); var persistedGrantDbContext = serviceScope.ServiceProvider.GetRequiredService <PersistedGrantDbContext>(); var configurationContext = serviceScope.ServiceProvider.GetRequiredService <ConfigurationDbContext>(); var databaseContext = serviceScope.ServiceProvider.GetRequiredService <DatabaseContext>(); // Migrate persistedGrantDbContext.Database.Migrate(); configurationContext.Database.Migrate(); databaseContext.Database.Migrate(); DatabaseContextSeed.SeedAsync(databaseContext).Wait(); ConfigurationDbContextSeed.SeedAsync(configurationContext).Wait(); }