Exemple #1
0
        public static async Task MigrateAndSeedShoppingDbContextAsync(IServiceProvider services)
        {
            var shoppingDbContext = services.GetRequiredService <ShoppingDbContext>();
            await shoppingDbContext.Database.MigrateAsync();

            var identityDbContext = services.GetRequiredService <IdentityContext>();
            await identityDbContext.Database.MigrateAsync();

            var userManager = services.GetRequiredService <UserManager <IdentityUser> >();
            await DatabaseSeed.SeedAsync(shoppingDbContext, userManager);
        }
Exemple #2
0
        public static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                var logger        = loggerFactory.CreateLogger <Program>();

                try
                {
                    logger.LogInformation("Start database migration...");

                    var budgetContext = services.GetRequiredService <ShoppingDbContext>();
                    await budgetContext.Database.MigrateAsync();

                    var identityContext = services.GetRequiredService <IdentityContext>();
                    await identityContext.Database.MigrateAsync();

                    logger.LogInformation("Finished database migration.");

                    logger.LogInformation("Start seed database...");
                    var userManager = services.GetRequiredService <UserManager <IdentityUser> >();
                    await DatabaseSeed.SeedAsync(budgetContext, userManager);

                    logger.LogInformation("Finished seeding database.");
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
            }

            host.Run();
        }