// 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(); } // runtime migrations using (IServiceScope scope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope()) { ECarsContext context = scope.ServiceProvider.GetService <ECarsContext>(); // mssql takes too long to start while (!CanConnect(context)) { System.Console.WriteLine("Can't connect to database, retrying"); Thread.Sleep(1000); } context.Database.Migrate(); if (context.Cars.Count() == 0) { context.Database.ExecuteSqlRaw(File.ReadAllText("./cars.sql")); } } app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); }
public static bool CanConnect(ECarsContext ctx) { try { return(ctx.Database.CanConnect()); } catch (SqlException) { return(false); } }