Esempio n. 1
0
        // Main() boots up a web server and the host for this project.
        public static void Main(string[] args)
        {
            // On the line below, the Host is declared and turned on.
            var host = BuildWebHost(args);

            // Creates the scope within which this program's services and operations are
            // executed.
            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                try
                {
                    // POPULATE THE DATABASE
                    // 1. Get a database context instance from the dependency injection container.
                    var context = services.GetRequiredService <Context>();
                    // 2. Call the seed method, passing to it the context.
                    DatabasePopulator.Initialize(context);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred while seeding the database.");
                }
                // 3. Dispose the context when the seed method is done and goes out of scope.
            }

            // The Host is started.
            host.Run();
        }