Esempio n. 1
0
        public async static Task Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

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

                var config = services.GetRequiredService <IConfiguration>();

                Log.Logger = new LoggerConfiguration()
                             .ReadFrom.Configuration(config)
                             .CreateLogger();

                try
                {
                    var dbContext   = services.GetRequiredService <ApplicationDbContext>();
                    var userManager = services.GetRequiredService <UserManager <ApplicationUser> >();

                    dbContext.Database.Migrate();
                    await ApplicationDbContextSeed.SeedDataAsync(dbContext);

                    await ApplicationDbContextSeed.SeedIdentityAsync(userManager);
                }
                catch (Exception ex)
                {
                    var logger = scope.ServiceProvider.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occured while migrating or seeding the database");
                }
            }

            host.Run();
        }