Ejemplo n.º 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>();
                var userManager   = services.GetRequiredService <UserManager <AppUser> >();
                var rolesManager  = services.GetRequiredService <RoleManager <IdentityRole> >();
                var configuration = services.GetRequiredService <IConfiguration>();

                try
                {
                    var context = services.GetRequiredService <RepositoryContext>();
                    await context.Database.MigrateAsync();

                    await ContextSeeder.SeedAsync(context, loggerFactory, userManager);

                    await RoleInitializer.InitializeAsync(userManager, rolesManager, configuration);

                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogInformation("Ok");
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError($"An error occurred during migration: {ex.Message}, {ex.StackTrace}");
                }
            }
            await host.RunAsync();
        }