/// <summary> /// Seed database with initial data. /// </summary> /// <param name="serviceProvider">Service provider.</param> public static void Initialize(IServiceProvider serviceProvider) { var configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: false) .Build(); var initialDbSeedEnable = Convert.ToBoolean(configuration.GetSection("InitialDbSeedEnable").Value); if (!initialDbSeedEnable) { Log.Information(InitializationConstants.SeedDisabled); return; } try { Log.Information(InitializationConstants.SeedEnabled); var contextOptions = serviceProvider.GetRequiredService <DbContextOptions <ApplicationDbContext> >(); using var applicationContext = new ApplicationDbContext(contextOptions); ApplicationDbContextSeeder.SeedAsync(applicationContext).GetAwaiter().GetResult(); Log.Information(InitializationConstants.SeedSuccess); } catch (Exception ex) { Log.Error(ex, InitializationConstants.SeedError); } }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ApplicationDbContextSeeder seeder) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); app.UseBrowserLink(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseIdentity(); // Add external authentication middleware below. To configure them please see https://go.microsoft.com/fwlink/?LinkID=532715 app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); seeder.SeedAsync().Wait(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { AutoMapperConfig.RegisterMappings(typeof(ErrorViewModel).GetTypeInfo().Assembly, typeof(PostCreateModel).GetTypeInfo().Assembly ); // Seed data on app launch using (var scope = app.ApplicationServices.CreateScope()) { var dbContext = scope.ServiceProvider.GetRequiredService <ApplicationDbContext>(); var dbContextSeeder = new ApplicationDbContextSeeder(); dbContextSeeder.SeedAsync(dbContext, scope.ServiceProvider).GetAwaiter().GetResult(); } if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } 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.UseHttpsRedirection(); app.UseStaticFiles(); app.UseSpaStaticFiles(); app.UseCookiePolicy(); app.UseRouting(); app.UseAuthentication(); app.UseIdentityServer(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller}/{action=Index}/{id?}"); endpoints.MapRazorPages(); endpoints.MapHub <ChatHub>("/chatHub"); endpoints.MapHub <NotificationsHub>("/notificationsHub"); }); app.UseSpa(spa => { spa.Options.SourcePath = "ClientApp"; if (env.IsDevelopment()) { spa.UseReactDevelopmentServer(npmScript: "start"); } }); }
public static IApplicationDbContext GetInMemoryContext() { var optionsbuilder = new DbContextOptionsBuilder <ApplicationDbContext>(); optionsbuilder.UseInMemoryDatabase("TodosInMemory"); var context = new ApplicationDbContext(optionsbuilder.Options, new MockedDateTimeProvider()); var seeder = new ApplicationDbContextSeeder(context); seeder.SeedAsync().Wait(); return(context); }
public async Task SeederTest() { var options = new DbContextOptionsBuilder <ApplicationDbContext>(). UseSqlServer(TestDatabaseConnectionProvider.GetConnectionStringDisposable()).Options; var context = new ApplicationDbContext(options); context.Database.Migrate(); var seeder = new ApplicationDbContextSeeder(); var logMock = new Mock <ILogger>(); var exception = await Record.ExceptionAsync(() => seeder.SeedAsync(context, logMock.Object)); Assert.Null(exception); context.Database.EnsureDeleted(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ApplicationDbContext context, IServiceProvider serviceProvider) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } 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.UseCors("MyAllowOrigins"); // app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); RoleManager <Role> roleManager = serviceProvider.GetRequiredService <RoleManager <Role> >(); UserManager <User> userManager = serviceProvider.GetRequiredService <UserManager <User> >(); ApplicationDbContextSeeder.SeedAsync(context, env, roleManager, userManager).Wait(); app.UseSwagger(); //enable swagger app.UseSwaggerUI(c => { c.RoutePrefix = "swagger"; //path naar de UI: /swagger/index.html c.SwaggerEndpoint("/swagger/v1/swagger.json", "AircraftIdentity_DB v1"); // c.SwaggerEndpoint("/swagger/v2/swagger.json", "Recipes_DB latest"); }); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); endpoints.MapRazorPages(); }); }