Beispiel #1
0
        private static void UpdateDatabase(IApplicationBuilder app,
                                           IConfiguration configuration)
        {
            // give me the scoped services (everyhting created by it will be closed at the end of service scope life).
            using var serviceScope = app.ApplicationServices
                                     .GetRequiredService <IServiceScopeFactory>()
                                     .CreateScope();

            using var ctx         = serviceScope.ServiceProvider.GetService <AppDbContext>();
            using var userManager = serviceScope.ServiceProvider.GetService <UserManager <AppUser> >();
            using var roleManager = serviceScope.ServiceProvider.GetService <RoleManager <AppRole> >();

            if (configuration["AppDataInitialization:DropDatabase"] == "True")
            {
                DataInitializers.DeleteDatabase(ctx);
            }

            if (configuration["AppDataInitialization:MigrateDatabase"] == "True")
            {
                DataInitializers.MigrateDatabase(ctx);
            }

            if (configuration["AppDataInitialization:SeedIdentity"] == "True")
            {
                DataInitializers.SeedIdentity(userManager, roleManager);
            }

            if (configuration.GetValue <bool>("AppDataInitialization:SeedData"))
            {
                DataInitializers.SeedData(userManager, ctx, configuration["ScreenDataPrefix"]);
            }
        }
Beispiel #2
0
        private static void SetupDatabase(IApplicationBuilder app, IWebHostEnvironment env,
                                          IConfiguration configuration)
        {
            using var serviceScope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope();

            using var ctx         = serviceScope.ServiceProvider.GetService <AppDbContext>() !;
            using var userManager = serviceScope.ServiceProvider.GetService <UserManager <AppUser> >() !;
            using var roleManager = serviceScope.ServiceProvider.GetService <RoleManager <AppRole> >() !;

            var logger = serviceScope.ServiceProvider.GetService <ILogger <Startup> >();

            if (configuration["DatabaseSetupConfiguration:DropDatabase"] == "True")
            {
                DataInitializers.DeleteDatabase(ctx, logger);
            }

            if (configuration["DatabaseSetupConfiguration:MigrateDatabase"] == "True")
            {
                DataInitializers.MigrateDatabase(ctx, logger);
            }

            if (configuration["DatabaseSetupConfiguration:SeedIdentity"] == "True")
            {
                DataInitializers.SeedIdentity(userManager, roleManager, logger, configuration);
            }

            if (configuration.GetValue <bool>("DatabaseSetupConfiguration:SeedData"))
            {
                DataInitializers.SeedData(ctx, logger);
            }
        }
Beispiel #3
0
        private static void UpdateDatabase(IApplicationBuilder app, IWebHostEnvironment env,
                                           IConfiguration configuration)
        {
            using var serviceScope = app
                                     .ApplicationServices
                                     .GetRequiredService <IServiceScopeFactory>()
                                     .CreateScope();
            using var context = serviceScope.ServiceProvider.GetService <AppDbContext>();

            using var userManager = serviceScope.ServiceProvider.GetService <UserManager <AppUser> >();
            using var roleManager = serviceScope.ServiceProvider.GetService <RoleManager <AppUserRole> >();

            var config = configuration.GetSection("AppDataInitialization");

            if (config.GetValue <bool>("DropDatabase"))
            {
                DataInitializers.DeleteDatabase(context);
            }

            if (config.GetValue <bool>("MigrateDatabase"))
            {
                DataInitializers.MigrateDatabase(context);
            }

            if (config.GetValue <bool>("SeedData"))
            {
                DataInitializers.SeedData(context);
            }

            if (config.GetValue <bool>("SeedIdentity"))
            {
                DataInitializers.SeedIdentity(userManager, roleManager);
            }
        }
Beispiel #4
0
        // CUSTOM CODE START
        private static void UpdateDatabase(IApplicationBuilder app, IWebHostEnvironment env, IConfiguration conf)
        {
            // Give me the scoped services (everything created by it will be closed at the end of the service scope life)
            using var serviceScope = app.ApplicationServices.GetRequiredService <IServiceScopeFactory>().CreateScope();
            // Use one context for the db in this whole function
            using var context = serviceScope.ServiceProvider.GetService <AppDbContext>();
            // Set up user and role managers for Identity
            using var userManager = serviceScope.ServiceProvider.GetService <UserManager <AppUser> >();
            using var roleManager = serviceScope.ServiceProvider.GetService <RoleManager <AppRole> >();
            // Log actions
            var logger = serviceScope.ServiceProvider.GetService <ILogger <Startup> >();

            // Here you can do whatever you need .. for example migrate each time so don't have to update db manually
            // context.Database.EnsureDeleted(); // Drop current db if you want to start from the scratch every time
            // context.Database.Migrate(); // Add the new migration. Will automatically create db if not there. If only this is needed don't do the dropping step.
            // These could also be done in configurations instead - ifs regarding it here

            if (conf.GetValue <bool>("DataInitialization:DropDatabase"))
            {
                logger.LogInformation("DropDatabase");
                DataInitializers.DeleteDatabase(context);
            }

            if (conf.GetValue <bool>("DataInitialization:MigrateDatabase"))
            {
                logger.LogInformation("MigrateDatabase");
                DataInitializers.MigrateDatabase(context);
            }

            if (conf.GetValue <bool>("DataInitialization:SeedIdentity"))
            {
                logger.LogInformation("SeedIdentity");
                DataInitializers.SeedIdentity(userManager, roleManager);
            }

            if (conf.GetValue <bool>("DataInitialization:SeedData"))
            {
                logger.LogInformation("SeedData");
                DataInitializers.SeedData(context, userManager);
            }
        }