Example #1
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                // Get all services
                var services = scope.ServiceProvider;
                try
                {
                    // Get DataContext from the services, and apply migrations
                    // Also creates the database if it doesn't already exist
                    var context     = services.GetRequiredService <KristematDbContext>();
                    var userManager = services.GetRequiredService <UserManager <AppUser> >();
                    var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >();
                    context.Database.Migrate();
                    Seed.Initialize(context, userManager, roleManager).Wait();
                }
                catch (Exception ex)
                {
                    // If an exception occured, get the loger from the services,
                    // and log the error to the terminal
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occured during migration");
                }

                host.Run();
            }
        }