public static void EnsureSeedData(string connectionString)
        {
            var services = new ServiceCollection();

            services.AddLogging();
            services.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseSqlServer(connectionString));

            services.AddIdentity <ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();
            services.AddOperationalDbContext(options => {
                options.ConfigureDbContext = db => {
                    db.UseSqlServer(connectionString,
                                    sql => sql.MigrationsAssembly(typeof(SeedData).Assembly.FullName));
                };
            });
            services.AddConfigurationDbContext(options => {
                options.ConfigureDbContext = db => db.UseSqlServer(connectionString,
                                                                   sql => { sql.MigrationsAssembly(typeof(SeedData).Assembly.FullName); });
            });
            using (var serviceProvider = services.BuildServiceProvider()) {
                using (var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope()) {
                    var context = scope.ServiceProvider.GetService <ApplicationDbContext>();
                    Console.WriteLine("Applying User database Migration");
                    context.Database.Migrate();
                }
            }

            Console.WriteLine("Applying Persisted Grants and Configuration database migrations");
            PersistanceMigrations.Migrate(connectionString);
        }
Esempio n. 2
0
        public static void EnsureSeedData(string connectionString)
        {
            var services = new ServiceCollection();

            services.AddLogging();
            services.AddDbContext <ApplicationDbContext>(options =>
                                                         options.UseSqlServer(connectionString));

            services.AddIdentity <ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores <ApplicationDbContext>()
            .AddDefaultTokenProviders();

            using (var serviceProvider = services.BuildServiceProvider()) {
                using (var scope = serviceProvider.GetRequiredService <IServiceScopeFactory>().CreateScope()) {
                    var context = scope.ServiceProvider.GetService <ApplicationDbContext>();
                    context.Database.Migrate();
                }
            }

            PersistanceMigrations.Migrate(connectionString);
        }