public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseCors("AllowSpecificOrigin"); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); using (var scope = app.ApplicationServices.CreateScope()) { var services = scope.ServiceProvider; var context = scope.ServiceProvider.GetService <UmbrellaShopContext>(); context.Database.EnsureDeleted(); context.Database.EnsureCreated(); var dbInitializer = services.GetService <IDbInitializer>(); dbInitializer.Seed(context); } } else { using (var scope = app.ApplicationServices.CreateScope()) { var context = scope.ServiceProvider.GetRequiredService <UmbrellaShopContext>(); IDbInitializer dbInitializer = scope.ServiceProvider.GetService <IDbInitializer>(); context.Database.EnsureCreated(); dbInitializer.Seed(context); } app.UseHsts(); } app.UseHttpsRedirection(); app.UseAuthentication(); app.UseMvc(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, IDbInitializer seeder) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseHsts(); } app.UseHttpsRedirection(); app.UseSwagger(); app.UseSwaggerUI(setupAction => { setupAction.SwaggerEndpoint(openApiSettings.GetSwaggerUrl(), openApiSettings.Name); }); app.UseMvc(); seeder.Seed().Wait(); }
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.ApplyConfiguration(new AuthorConfiguration()); modelBuilder.ApplyConfiguration(new BookConfiguration()); _dbInitializer.Seed(modelBuilder); base.OnModelCreating(modelBuilder); }
/// <summary> /// This adds admin user and some seed users with random transaction /// UserNames: /// [email protected] /// [email protected] /// [email protected] /// [email protected] /// [email protected] /// [email protected] /// [email protected] /// [email protected] /// </summary> public static void SeedDatabase(this IApplicationBuilder app, string userPassword) { ILogger logger = LogManager.GetCurrentClassLogger(); using IServiceScope scope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope(); IServiceProvider services = scope.ServiceProvider; try { IDbInitializer dbInitializer = services.GetRequiredService <IDbInitializer>(); dbInitializer.Seed(userPassword); } catch (Exception ex) { logger.Error(ex, ex.Message); return; } }
public void Seed() { _dbInitializer.Seed(); }