public static void Main(string[] args)
        {
            //It creates the hostmachine
            //We are asking host to build the containers, When you start this project.
            //so after the successful build we need to seed the data
            var host = CreateHostBuilder(args).Build();

            //Now lets call the host and ask for the services.
            //CreateScope() mean get me the access to all the services that are available with in my host.
            //scope is a huge memory variable.
            using (var scope = host.Services.CreateScope())
            {
                //The variable scope is wrapped by "using" statement
                //What ever we write the code inside this, is the only code that can use this varible "scope"
                //the scope of the varible is limited in this using block.

                /*WHat is the purpose of using statement??  It guarantee that WHen you get out of this curly brackets
                 * It is going to destroy scope variable, which means your memory will be released*/

                /*Now we can ask the scope that, can you tell me all the service providers ,that is available
                 * for each scope*/
                var serviceProviders = scope.ServiceProvider;//we are getting all the serviceproviders

                /*Out of those service providers, can you get the acces to this requiredService which is
                 * provided by catalog context*/
                var context = serviceProviders.GetRequiredService <OrdersContext>();

                //Now we know that database is created
                //we can ensure if it is created
                MigrateDatabase.EnsureCreated(context);
            }
            host.Run();
        }
Esempio n. 2
0
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, MigrateDatabase migrateDatabase, ILogger <Startup> logger)
        {
            try
            {
                migrateDatabase();
            }
            catch (Exception exc)
            {
                logger.LogError(0, exc, "Error during database migration");

                throw;
            }

            app.UseSwagger();
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "Time Logger");
            });

            app.UseRouting();
            app.UseAuthorization();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
                endpoints.MapHealthChecks("/health");
            });
        }
Esempio n. 3
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var serviceProviders = scope.ServiceProvider;
                var context          = serviceProviders.GetRequiredService <OrdersContext>();
                MigrateDatabase.EnsureCreated(context);
            }
            host.Run();
        }
Esempio n. 4
0
        public static void Main(string[] args)
        {
            var host = BuildWebHost(args);

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                try
                {
                    var context = services.GetRequiredService <OrdersContext>();
                    MigrateDatabase.EnsureCreated(context);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred while seeding the database.");
                }
            }
            host.Run();
        }
        public static void Main(string[] args)
        {
            IWebHost host = CreateWebHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                IServiceProvider services = scope.ServiceProvider;
                try
                {
                    var context = services.GetRequiredService <CatalogContext>();
                    MigrateDatabase.EnsureCreated(context);
                    CatalogSeed.SeedAsync(context).Wait();
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occured while seeding the database.");
                }
            }
            host.Run();
        }
Esempio n. 6
0
        private static IDatabaseInitializer <Context> GetInitializerStrategy(BuildType buildType)
        {
            IDatabaseInitializer <Context> strategy = null;

            switch (buildType)
            {
            case BuildType.Migrate:
                strategy = new MigrateDatabase();
                break;

            case BuildType.Rebuild:
                strategy = new ForceSingleUserInitializer <Context>(new RebuildDatabase());
                break;

            default:
                strategy = new NullDatabase();
                break;
            }

            return(strategy);
        }