Esempio n. 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();
            if (!context.Role.Any())
            {
                // seed roles
                context.AddRange(Seed.SeedRoles());
                context.SaveChanges();
            }
            List <Role> roles = context.Role.ToList();

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

                if (!env.IsProduction())
                {
                    // Seed institutions
                    context.Institution.Add(Seed.SeedInstitution());
                    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 (!env.IsProduction())
            {
                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();
                }
            }
        }