public static IWebHost PopulateData(this IWebHost webhost) { using (var scope = webhost.Services.GetService <IServiceScopeFactory>().CreateScope()) { using (var dbContext = scope.ServiceProvider.GetRequiredService <DataContext>()) { ProductsInitializer.SeedProducts(dbContext); } } return(webhost); }
protected override void Seed(E_Commerce.DAL.ProductsContext context) { ProductsInitializer.SeedProductsData(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. E.g. // // context.People.AddOrUpdate( // p => p.FullName, // new Person { FullName = "Andrew Peters" }, // new Person { FullName = "Brice Lambson" }, // new Person { FullName = "Rowan Miller" } // ); // }
public static void Main(string[] args) { var host = CreateWebHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { try { var context = scope.ServiceProvider.GetService <IProductsDbContext>(); var concreteContext = (ProductsDbContext)context; concreteContext.Database.Migrate(); ProductsInitializer.Initialize(concreteContext); } catch (Exception ex) { var logger = scope.ServiceProvider.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred while migrating or initializing the database."); } } host.Run(); }
public static void Main(string[] args) { var host = CreateWebHostBuilder(args).Build(); using (var scope = host.Services.CreateScope()) { CultureInfo.CurrentCulture = new CultureInfo("fr-FR"); var services = scope.ServiceProvider; var userManager = services.GetService <UserManager <IdentityUserDefaultPwd> >(); var roleManager = services.GetService <RoleManager <IdentityRole> >(); var context = services.GetRequiredService <ApplicationDbContext>(); context.Database.Migrate(); // requires using Microsoft.Extensions.Configuration; var config = host.Services.GetRequiredService <IConfiguration>(); // Set password with the Secret Manager tool. // dotnet user-secrets set SeedUserPW <pw> #region Seed DB var logger = services.GetRequiredService <ILogger <Program> >(); var testUserPw = config["SeedUserPW"]; try { UserDataInitializer.SeedData(context).Wait(); } catch (Exception ex) { logger.LogError(ex.Message, "An error occurred seeding the UserData DB."); } try { IdentityInitializer.SeedData(userManager, roleManager).Wait(); } catch (Exception ex) { logger.LogError(ex.Message, "An error occurred seeding the Identity DB."); } try { PromsKeyboardShortcutInitializer.SeedData(context).Wait(); } catch (Exception ex) { logger.LogError(ex.Message, "An error occurred seeding the PromsKeyboardShortcut DB."); } try { SystemParametersInitializer.SeedData(context).Wait(); } catch (Exception ex) { logger.LogError(ex.Message, "An error occurred seeding the SystemParameters DB."); } try { ProductsInitializer.SeedData(context).Wait(); } catch (Exception ex) { logger.LogError(ex.Message, "An error occurred seeding the ProductsInitializer DB."); } try { TransactionHistoryInitializer.SeedData(context).Wait(); } catch (Exception ex) { logger.LogError(ex.Message, "An error occurred seeding the TransactionHistory DB."); } #endregion } host.Run(); CreateWebHostBuilder(args) .Build().Run(); }