コード例 #1
0
 public static void Main(string[] args)
 {
     CreateHostBuilder(args)
     .Build()
     .MigrateDbContext(StoreDbContextSeed.Seed())
     .Run();
 }
コード例 #2
0
ファイル: Program.cs プロジェクト: psynames/e-Kommerce
        public static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            #region Comments on using()

            //any code running inside a using statement is disposed when done running
            //this is cos we are outside of the startup class wjere we do not have
            //controll of the life time of the created instances.
            //this is coz we are not depending on asp.net core to handle the disposation of such code lifetime

            #endregion

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                #region Comments on ILoggrFactory

                //this is to log information out in the console
                //loggerfactory allows us to create instances of the logger

                #endregion

                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var context = services.GetRequiredService <StoreDbContext>();

                    //will create the database if it does not already exist
                    await context.Database.MigrateAsync();

                    await StoreDbContextSeed.SeedAsync(context, loggerFactory);
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An Error Occured During Migraion");
                }
            }

            host.Run();
        }