// 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("/Home/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.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); // Make sure database is setup and seeded with data using (var serviceScope = app.ApplicationServices.CreateScope()) { var dbContext = serviceScope.ServiceProvider.GetService <ProductContext>(); ProductDatabaseInitializer.Configure(dbContext); } }
// 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(); } app.UseHttpsRedirection(); app.UseSwagger(); app.UseSwaggerUI(options => { options.SwaggerEndpoint("/swagger/v1/swagger.json", "Offers API v1"); options.RoutePrefix = string.Empty; }); app.UseRouting(); app.UseCors(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); // Make sure database is setup and seeded with data using (var serviceScope = app.ApplicationServices.CreateScope()) { var dbContext = serviceScope.ServiceProvider.GetService <ProductContext>(); ProductDatabaseInitializer.Configure(dbContext); } }
public static void Main(string[] args) { var host = CreateWebHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { var context = services.GetRequiredService <ProductContext>(); ProductDatabaseInitializer.Seed(context); } catch (Exception ex) { var logger = services.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred while seeding the database."); } } host.Run(); }
public IQueryable <Category> GetCategories() { IQueryable <Category> query = ProductDatabaseInitializer.GetDefaultCategories().AsQueryable(); return(query); }