Beispiel #1
0
 /// <summary>
 /// Updates the database.
 /// </summary>
 /// <param name="app">The application.</param>
 /// <param name="env">The environmental variable.</param>
 private static void UpdateDatabase(IApplicationBuilder app, IWebHostEnvironment env)
 {
     using IServiceScope serviceScope = app.ApplicationServices
                                        .GetRequiredService <IServiceScopeFactory>()
                                        .CreateScope();
     using IdentityDbContext context = serviceScope.ServiceProvider.GetService <IdentityDbContext>();
     context.Database.Migrate();
     if (!context.IdentityUser.Any())
     {
         context.IdentityUser.AddRange(TestUsers.GetDefaultIdentityUsers(env.IsProduction()));
         context.SaveChanges();
     }
 }
Beispiel #2
0
        /// <summary>
        /// Updates the database.
        /// </summary>
        /// <param name="app">The application.</param>
        /// <param name="env">The environmental variable.</param>
        private static void UpdateDatabase(IApplicationBuilder app, IWebHostEnvironment env)
        {
            using IServiceScope serviceScope = app.ApplicationServices
                                               .GetRequiredService <IServiceScopeFactory>()
                                               .CreateScope();
            using IdentityDbContext context = serviceScope.ServiceProvider.GetService <IdentityDbContext>();
            context.Database.Migrate();
            List <IdentityUser> identityUsers = TestUsers.GetDefaultIdentityUsers();

            foreach (IdentityUser identityUser in identityUsers.Where(identityUser => !context.IdentityUser.Any(e => e.SubjectId == identityUser.SubjectId)))
            {
                if (env.IsProduction())
                {
                    identityUser.Password = TestUsers.CreateTestUserPassword(identityUser.Username);
                }
                context.Add(identityUser);
            }
            context.SaveChanges();
        }