Example #1
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>();
                try
                {
                    var context = services.GetRequiredService <ApplicationDbContext>();
                    await context.Database.MigrateAsync();

                    await ApplicationDbSeed.SeedAsync(context, loggerFactory);

                    // Identity
                    var userManager     = services.GetRequiredService <UserManager <AppUser> >();
                    var identityContext = services.GetRequiredService <AppIdentityDbContext>();
                    await identityContext.Database.MigrateAsync();

                    await AppIdentityDbContextSeed.SeedUserAsync(userManager);
                }
                catch (Exception ex)
                {
                    // <Program> thats specify class what we want to log all errors and
                    // pass them in exception (ex)
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occured during migration");
                }
            }
            host.Run();
        }
Example #2
0
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args)
                       .Build();

            using (var scope = host.Services.CreateScope())
            {
                var services    = scope.ServiceProvider;
                var userManager = services.GetRequiredService <UserManager <ApplicationUser> >();
                var context     = services.GetRequiredService <ApplicationDbContext>();
                ApplicationDbSeed.SeedAsync(context, userManager)
                .Wait();
            }

            host.Run();
        }