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

            await DataHelper.ManageDataAsync(host);

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var context      = services.GetRequiredService <ApplicationDbContext>();
                    var userManager  = services.GetRequiredService <UserManager <FPUser> >();
                    var roleManager  = services.GetRequiredService <RoleManager <IdentityRole> >();
                    var imageService = services.GetRequiredService <IImageService>();
                    await ContextSeed.SeedRolesAsync(roleManager);

                    await ContextSeed.SeedDefaultUserAsync(userManager, imageService);
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
            }
            host.Run();
        }
Beispiel #2
0
        //Handles the Migration Programmatic
        public static async Task ManageDataAsync(IHost host)
        {
            try
            {
                //This technique is used to obtain references to services
                //Normally I would just inject these services but you cant use a constructor in a static class.
                using var svcScope = host.Services.CreateScope();
                var svcProvider = svcScope.ServiceProvider;

                //The services  will run your migrations
                var dbContextSvc = svcProvider.GetRequiredService <ApplicationDbContext>();
                await dbContextSvc.Database.MigrateAsync();

                var userManager  = svcProvider.GetRequiredService <UserManager <FPUser> >();
                var roleManager  = svcProvider.GetRequiredService <RoleManager <IdentityRole> >();
                var imageService = svcProvider.GetRequiredService <IImageService>();
                await ContextSeed.SeedRolesAsync(roleManager);

                await ContextSeed.SeedDefaultUserAsync(userManager, imageService);

                //var settings = svcProvider.GetRequiredService<IOptions<AdminSettings>>();

                //await SeedDataAsync(seedConfigSvc);
            }
            catch (PostgresException ex)
            {
                Console.WriteLine($"Exception while running Manage Data => {ex}");
            }
        }