public static async Task Main(string[] args) { var host = CreateHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { var context = services.GetRequiredService <ApplicationDbContext>(); if (context.Database.IsSqlServer()) { context.Database.Migrate(); } await ApplicationDbContextSeed.Initialize(services, context); } catch (Exception ex) { var logger = scope.ServiceProvider.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred while migrating or seeding the database."); throw; } } await host.RunAsync(); }
public static async Task SetupAll() { using var scope = _scopeFactory.CreateScope(); var context2 = scope.ServiceProvider.GetService <ApplicationDbContext>(); await ApplicationDbContextSeed.Initialize(scope.ServiceProvider, context2); }
private static void CreateDbIfNotExists(IHost host) { using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { var context = services.GetRequiredService <ApplicationDbContext>(); ApplicationDbContextSeed.Initialize(context); } catch (Exception ex) { var logger = services.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred creating the DB."); } } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } app.UseCustomExceptionHandler(); app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseSpaStaticFiles(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller}/{action=Index}/{id?}"); }); app.UseSpa(spa => { spa.Options.SourcePath = "ClientApp"; if (env.IsDevelopment()) { spa.UseReactDevelopmentServer(npmScript: "start"); } }); ApplicationDbContextSeed.Initialize(app.ApplicationServices); }