public void BlogInitializer_Adds_And_Persists_Data_To_Database() { // Arrange // Use a new database so that our results are not interfered with by other tests. // If this is the first test to run, the shared SQL LocalDB instance will be created now. string connectionString = TestSetup.GetConnectionStringForNewDatabase(); BlogInitializer target = new BlogInitializer(); using (BlogContext context = new BlogContext(connectionString)) { // Act target.InitializeDatabase(context); } int actual; using (BlogContext context = new BlogContext(connectionString)) { // Assert actual = context.Posts.Count(); } Assert.AreEqual(2, actual); }
protected override void Seed(Blog.DAL.BlogContext context) { BlogInitializer.SeedBlogData(context); // This method will be called after migrating to the latest version. // You can use the DbSet<T>.AddOrUpdate() helper extension method // to avoid creating duplicate seed data. }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, UserManager <AppUser> userManager, RoleManager <AppRole> roleManager, IBlogService blogService, ICategoryService categoryService, ICategoryBlogService categoryBlogService) { 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.UseStatusCodePagesWithReExecute("/Home/StatusCode", "?code={0}"); app.UseHttpsRedirection(); app.UseRouting(); app.UseStaticFiles(); var locOptions = app.ApplicationServices.GetService <IOptions <RequestLocalizationOptions> >(); app.UseRequestLocalization(locOptions.Value); app.UseAuthentication(); app.UseAuthorization(); IdentityInitializer.SeedData(userManager, roleManager).Wait(); BlogInitializer.SeedData(blogService, categoryService, categoryBlogService).Wait(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "areas", pattern: "{area}/{controller=Home}/{action=Index}/{id?}"); endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); }