public static async Task <IHost> ApplyMigrations(this IHost host) { using (IServiceScope scope = host.Services.CreateScope()) { IServiceProvider services = scope.ServiceProvider; ILoggerFactory loggerFactory = services.GetRequiredService <ILoggerFactory>(); try { AppDbContext appContext = services.GetRequiredService <AppDbContext>(); await appContext.Database.MigrateAsync(); UserManager <User> userManager = services.GetRequiredService <UserManager <User> >(); RoleManager <Role> roleManager = services.GetRequiredService <RoleManager <Role> >(); AppIdentityDbContext identityContext = services.GetRequiredService <AppIdentityDbContext>(); await identityContext.Database.MigrateAsync(); await SeedUserAndRole.SeedUserAndRoleAsync(userManager, roleManager); } catch (Exception ex) { ILogger <Program> logger = loggerFactory.CreateLogger <Program>(); logger.LogError(ex, "An error occured during migration"); } } return(host); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public async void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseBrowserLink(); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Home/Error"); } app.UseStaticFiles(); app.UseAuthentication(); app.UseSession(); app.UseMvc(routes => { routes.MapRoute( name: "areas", template: "{area:exists}/{controller=Home}/{action=Index}/{id?}" ); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{id?}"); }); await SeedUserAndRole.EnsurePopulateRole(app); // appelle les methode pour ajouter des comptes dans ta base await SeedUserAndRole.EnsurePopulateUserAdmin(app); // erreur car le provider default , il sait pas si cest en transiant/scope/singleton donc on rajoute dans la classe programme comment ils fonctionne }