Пример #1
0
        /// <summary>
        ///     Updates the database.
        /// </summary>
        /// <param name="app">The application.</param>
        /// <param name="env">The env.</param>
        private static void UpdateDatabase(IApplicationBuilder app, IWebHostEnvironment env)
        {
            using IServiceScope serviceScope = app.ApplicationServices
                                               .GetRequiredService <IServiceScopeFactory>()
                                               .CreateScope();
            using ApplicationDbContext context = serviceScope.ServiceProvider.GetService <ApplicationDbContext>();
            context.Database.Migrate();

            // Check if Roles and RoleScopes in DB matches seed, if it doesn't match: database is updated.
            SeedHelper.InsertRoles(Seed.SeedRoles(), context);
            List <Role> roles = context.Role.ToList();

            if (!env.IsProduction())
            {
                if (!context.Institution.Any())
                {
                    // Seed institutions
                    context.Institution.AddRange(Seed.SeedInstitution());
                    context.SaveChanges();
                }

                if (!context.User.Any())
                {
                    // seed admin
                    context.User.Add(Seed.SeedAdminUser(roles));
                    context.SaveChanges();

                    //Seed random users
                    context.User.Add(Seed.SeedPrUser(roles));
                    context.User.AddRange(Seed.SeedUsers(roles));
                    context.User.Add(Seed.SeedDataOfficerUser(roles));
                    context.SaveChanges();
                }

                if (!context.Project.Any())
                {
                    //Seed projects
                    List <User> users = context.User.ToList();
                    context.Project.AddRange(Seed.SeedProjects(users));
                    context.SaveChanges();
                }
                if (!context.Collaborators.Any())
                {
                    //seed collaborators
                    List <Project> projects = context.Project.ToList();
                    context.Collaborators.AddRange(Seed.SeedCollaborators(projects));
                    context.SaveChanges();
                }
                if (!context.Highlight.Any())
                {
                    List <Project> projects = context.Project.ToList();
                    context.Highlight.AddRange(Seed.SeedHighlights(projects));
                    context.SaveChanges();
                }

                // TODO seed embedded projects
            }


            // Seed call to action options
            List <CallToActionOption> options = Seed.SeedCallToActionOptions();

            foreach (CallToActionOption callToActionOption in options)
            {
                if (!context.CallToActionOption.Any(s => s.Type == callToActionOption.Type &&
                                                    s.Value == callToActionOption.Value))
                {
                    context.CallToActionOption.Add(callToActionOption);
                    context.SaveChanges();
                }
            }

            if (!context.Category.Any())
            {
                context.Category.AddRange(Seed.SeedCategories());
                context.SaveChanges();
            }

            if (!context.WizardPage.Any())
            {
                context.WizardPage.AddRange(Seed.SeedWizardPages());
                context.SaveChanges();
            }
            if (!context.DataSource.Any())
            {
                context.DataSource.AddRange(Seed.SeedDataSources());
                context.SaveChanges();
            }

            SeedHelper.SeedDataSourceWizardPages(context);
        }