// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, RoleManager <MyIdentityRole> roleManager) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseMigrationsEndPoint(); } 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.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapRazorPages(); endpoints.MapControllerRoute( name: "areas", pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}" ); }); // seed the Application DB Context ApplicationDbContextSeed.SeedRolesAsync(roleManager).Wait(); }
public async static Task Main(string[] args) { var host = CreateHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { var context = services.GetRequiredService <ApplicationDbContext>(); var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >(); var userManager = services.GetRequiredService <UserManager <AppUser> >(); if (context.Database.IsNpgsql()) { context.Database.Migrate(); } await ApplicationDbContextSeed.SeedRolesAsync(roleManager); await ApplicationDbContextSeed.SeedDefaultUserAsync(userManager); await ApplicationDbContextSeed.SeedSampleDataAsync(context); } catch (Exception ex) { var logger = services.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred while migrating or seeding the database."); } } await host.RunAsync(); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, RoleManager <MyIdentityRole> roleManager) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseMigrationsEndPoint(); } else { app.UseExceptionHandler("/Error"); //use automatic page handler to throw error page // 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(); // if you enter http, it will redirect to https app.UseStaticFiles(); // allow you to use the things in wwwroot app.UseRouting(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => // this is where you map 2 different styles of endpoints { endpoints.MapRazorPages(); //another route, look for razor pages, and map those routes // be careful not to have a razor page match a default route eg (dont create home razor page for the home controller) // you dont want to have the same naming like that endpoints.MapControllerRoute( //mapcontrollerroute = asp.net mvc routing name: "areas", pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}" ); }); // seed the applicationdbcontext //async then wait for the class task = forces the running async code to run synchronously ApplicationDbContextSeed.SeedRolesAsync(roleManager).Wait(); //await = method called asynchronously // remove await, and write Wait() at the back }