public static void Main(string[] args) { //1. Get Host var host = CreateWebHostBuilder(args).Build(); //2. Find the service layer within our scope. using (var scope = host.Services.CreateScope()) { //3. Get the instance of CustomerDBContext in our services layer var services = scope.ServiceProvider; var context = services.GetRequiredService <CustomerDBContext>(); //4. Call the SeedCustomerData to create sample data SeedCustomerData.Initialize(services); } //Continue to run the application host.Run(); }
public static void Main(string[] args) { var host = BuildWebHost(args); using (var scope = host.Services.CreateScope()) { var services = scope.ServiceProvider; try { SeedCustomerData.Initialize(services); } catch (Exception ex) { var logger = services.GetRequiredService <ILogger <Program> >(); logger.LogError(ex, "An error occurred seeding the DB."); } } host.Run(); }