public static async Task Main(string[] args) { IHost host = CreateHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { IServiceProvider provider = scope.ServiceProvider; try { var userManager = provider.GetRequiredService <UserManager <User> >(); var roleManager = provider.GetRequiredService <RoleManager <IdentityRole> >(); var configuration = provider.GetRequiredService <IConfiguration>(); await RoleInitializer.InitializeRolesAsync(roleManager, configuration); await AdminInitializer.InitializeAdmin(userManager, configuration); } catch (Exception ex) { var logger = provider.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "Произошла ошибка при работе с БД"); } } host.Run(); }
public void Configure(IApplicationBuilder app, IHostingEnvironment env, SayDbContext context) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); app.UseDatabaseErrorPage(); } else { app.UseExceptionHandler("/Home/Error"); app.UseHsts(); } context.Database.Migrate(); app.UseStaticFiles(); app.UseStatusCodePages(); app.UseSession(); app.UseAuthentication(); app.UseMvc(routes => { routes.MapRoute( name: "categoryfilter", template: "Product/{action}/{category?}", defaults: new { Controller = "Product", action = "List" }); routes.MapRoute( name: "default", template: "{controller=Home}/{action=Index}/{Id?}"); }); DbInitializer.Seed(app); //AdminInitializer.CreateRoles(service, _configurationRoot).Wait(); AdminInitializer.InitializeAsync(app, _configurationRoot).Wait(); }
public static async Task Main(string[] args) { var host = CreateHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { await AdminInitializer.Initializer(scope.ServiceProvider); } await host.RunAsync(); }
protected override void Seed(Contexts.ToolContext 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. DepartmentInitializer.Initialize(context); RoleInitializer.Initialize(context); StockInitializer.Initialize(context); AdminInitializer.Initialize(context); StockKeeperInitializer.Initialize(context); context.SaveChanges(); }
public static void Main(string[] args) { var host = CreateHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; var serviceProvider = services.GetRequiredService <IServiceProvider>(); var configuration = services.GetRequiredService <IConfiguration>(); AdminInitializer.CreateRoles(serviceProvider, configuration).Wait(); } host.Run(); }
public static async Task Main(string[] args) { var host = CreateHostBuilder(args).Build(); using var scope = host.Services.CreateScope(); var services = scope.ServiceProvider; try { var userManager = services.GetRequiredService <UserManager <User> >(); var rolesManager = services.GetRequiredService <RoleManager <IdentityRole> >(); await AdminInitializer.SeedAdminUser(rolesManager, userManager); } catch (Exception ex) { var logger = services.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "Возникло исключение при инициализации ролей."); } host.Run(); }
public static async Task Main(string[] args) { var host = CreateHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { var userManager = services.GetRequiredService <UserManager <ApplicationUser> >(); await AdminInitializer.InitializeAsync(userManager); } catch (Exception ex) { var logger = services.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred while seeding the database."); } } host.Run(); }