private static object DoMigration() { Console.WriteLine("Database connection: " + AppConfigurations.Configuration.GetConnectionString("Default")); Console.WriteLine("Continue to migration for this database?(Y/N):"); var command = Console.ReadLine(); if (command?.ToLower() != "y") { Console.WriteLine("Migration canceled."); return(false); } try { Console.WriteLine("Database migration started..."); var dbContext = new AppDbContextFactory().CreateDbContext(null); dbContext.Database.Migrate(); Console.WriteLine("Database migration completed."); Console.WriteLine("Database seed started..."); var seeder = new AppDbSeeder(dbContext); seeder.Seed(); Console.WriteLine("Database seed completed."); return(true); } catch (Exception e) { Console.WriteLine("An error occured during migration:"); Console.WriteLine(e); return(false); } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); app.UseHsts(); } app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseSpaStaticFiles(); app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller}/{action=Index}/{id?}"); }); app.UseSpa(spa => { // To learn more about options for serving an Angular SPA from ASP.NET Core, // see https://go.microsoft.com/fwlink/?linkid=864501 spa.Options.SourcePath = "ClientApp"; if (env.IsDevelopment()) { spa.UseAngularCliServer(npmScript: "start"); } }); using (var serviceScope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory> ().CreateScope()) { var dbContext = serviceScope.ServiceProvider.GetService <AppDbContext> (); dbContext.Database.Migrate(); AppDbSeeder.Seed(dbContext); } }