public static int Main(string[] args) { var configuration = GetConfiguration(); Log.Logger = CreateSerilogLogger(configuration); try { Log.Information("Configuring web host ({ApplicationContext})...", AppName); var host = CreateWebHostBuilder(args).Build(); Log.Information("Applying migrations ({ApplicationContext})...", AppName); host.MigrateDbContext <PersistedGrantDbContext>((_, __) => { }) .MigrateDbContext <ApplicationDbContext>((context, services) => { var env = services.GetService <IHostingEnvironment>(); var logger = services.GetService <ILogger <ApplicationDbContextSeed> >(); var settings = services.GetService <IOptions <AppSettings> >(); var serviceProvider = services.GetRequiredService <IServiceProvider>(); var contextSeed = new ApplicationDbContextSeed(); contextSeed.SeedAsync(context, env, logger, settings).Wait(); // create roles and set role into users contextSeed.CreateUserRoles(serviceProvider).Wait(); // Create claims contextSeed.InitUserClaims(serviceProvider).Wait(); }) .MigrateDbContext <ConfigurationDbContext>((context, services) => { new ConfigurationDbContextSeed() .SeedAsync(context, configuration) .Wait(); }); Log.Information("Starting web host ({ApplicationContext})...", AppName); host.Run(); return(0); } 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(); * }*/ } }