Beispiel #1
0
        public static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            // scope declaration to bring the services to be used
            using var scope = host.Services.CreateScope();
            var services = scope.ServiceProvider;

            try
            {
                // DataContext service to seed the data
                var context = services.GetRequiredService <DataContext>();
                await context.Database.MigrateAsync();

                await Seed.SeedCategories(context);

                await Seed.SeedUsers(context);
            }
            catch (Exception ex)
            {
                // Logger service to log any error
                var logger = services.GetRequiredService <ILogger <Program> >();
                logger.LogError(ex, "An Error Ocurred during data Seed.");
            }

            // this will run the application after seeding
            await host.RunAsync();
        }
Beispiel #2
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                try
                {
                    var context = services.GetRequiredService <BlogContext>();

                    context.Database.Migrate();

                    Seed.SeedCategories(context);
                    Seed.SeedArticles(context);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occured during migration");
                }
            }
            host.Run();
        }
        /// <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);
        }