Esempio n. 1
0
        public static async Task <bool> MigrateDB(IHost host)
        {
            using var scope = host.Services.CreateScope();
            var services = scope.ServiceProvider;
            var logger   = scope.ServiceProvider.GetRequiredService <ILogger <Program> >();

            try
            {
                var context = services.GetRequiredService <ApplicationDbContext>();
                var env     = services.GetRequiredService <IWebHostEnvironment>();

                context.Database.Migrate();

                await ApplicationDbContextSeed.SeedDbAsync(context);

                logger.LogInformation("Migrating db");

                return(true);
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "An error occurred while migrating or seeding the database.");
                return(false);
            }
        }