Exemple #1
0
        public static async Task Main(string[] args)
        {
            // Added this logic to update and seed database when we run migrations, no need to run database update after a migration
            CreateHostBuilder(args).Build();
            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 <DzDbContext>();
                    await context.Database.MigrateAsync();

                    await DzContextSeed.SeedDzDb(context, loggerFactory);

                    // To seed identity db
                    var userManager     = services.GetRequiredService <UserManager <AppUser> >();
                    var identityContext = services.GetRequiredService <AppIdentityDbContext>();
                    await identityContext.Database.MigrateAsync();

                    await IdentityDbContextSeed.SeedUsersAsync(userManager);
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occured during migration");
                }
            }

            host.Run();
        }
        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 userManager     = services.GetRequiredService <UserManager <ApplicationUser> >();
                    var roleManager     = services.GetRequiredService <RoleManager <IdentityRole> >();
                    var identityContext = services.GetRequiredService <IdentityDbContext>();
                    await IdentityDbContextSeed.SeedAsync(userManager, roleManager, identityContext);
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
                try
                {
                    var dataContext = services.GetRequiredService <ProjectDbContext>();
                    var syncTables  = services.GetRequiredService <ISyncTables>();
                    await ProjectDbContextSeed.SeedAsync(dataContext, syncTables);
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
                host.Run();
            }
        }
        public async static Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                try
                {
                    var identityContext = services.GetRequiredService <IdentityContext>();

                    if (identityContext.Database.IsSqlite())
                    {
                        identityContext.Database.Migrate();
                    }

                    var context = services.GetRequiredService <ApplicationDbContext>();

                    if (context.Database.IsSqlServer())
                    {
                        context.Database.Migrate();
                    }

                    var userManager = services.GetRequiredService <UserManager <ApplicationUser> >();

                    await IdentityDbContextSeed.SeedDefaultUserAsync(userManager);

                    await ApplicationDbContextSeed.SeedSampleDataAsync(context);
                }
                catch (Exception ex)
                {
                    var logger = scope.ServiceProvider.GetRequiredService <ILogger <Program> >();

                    logger.LogError(ex, "An error occurred while migrating or seeding the database.");

                    throw;
                }
            }

            await host.RunAsync();
        }