Ejemplo n.º 1
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                try
                {
                    var identityContext = services.GetRequiredService <ApplicationDbContext>();
                    identityContext.Database.Migrate();
                    ApplicationSeedData.SeedAsync(identityContext, services).Wait();

                    var context = services.GetRequiredService <JobBankContext>();
                    context.Database.Migrate();
                    JBSeedData.Initialize(services);
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
            }

            host.Run();
        }
Ejemplo n.º 2
0
        public static void Main(string[] args)
        {
            var host = CreateWebHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                try
                {
                    var context = services.GetRequiredService <NBDContext>();
                    context.Database.Migrate();
                    MCSeedData.Initialize(services);
                    var identitycontext = services.GetRequiredService <ApplicationDbContext>(); // gets identity context
                    identitycontext.Database.Migrate();
                    ApplicationSeedData.SeedAsync(identitycontext, services).Wait();            // call seed data and seed users
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occured seeding data");
                }
            }
            host.Run();
        }
Ejemplo n.º 3
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                try
                {
                    #region Order is important. Identity should always come before main context
                    var identityContext = services.GetRequiredService <ApplicationDbContext>();
                    identityContext.Database.Migrate();
                    ApplicationSeedData.SeedAsync(identityContext, services).Wait();

                    var hagerContext = new HagerContext(
                        services.GetRequiredService <DbContextOptions <HagerContext> >());
                    hagerContext.Database.Migrate();
                    HagerSeedData.Seed(hagerContext, identityContext);
                    #endregion
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
            }

            host.Run();
        }
Ejemplo n.º 4
0
        public static async Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;

                try
                {
                    var applicationContext = services.GetRequiredService <ApplicationDbContext>();
                    var userManager        = services.GetRequiredService <UserManager <ApplicationUser> >();
                    var roleManager        = services.GetRequiredService <RoleManager <ApplicationRole> >();
                    await ApplicationSeedData.SeedAsync(applicationContext, userManager, roleManager);

                    var userRoles = new List <string>
                    {
                        "Admin",
                        "Senior",
                        "Staff",
                        "Junior"
                    };

                    var userEmails = new List <string>
                    {
                        "*****@*****.**",
                        "*****@*****.**",
                        "*****@*****.**",
                        "*****@*****.**"
                    };
                    var projects = ApplicationSeedData.GetPreconfiguredProjects().ToList();
                    var user1    = userManager.FindByEmailAsync("*****@*****.**");
                    if (user1.Result == null)
                    {
                        for (int i = 0; i < 4; i++)
                        {
                            await roleManager.CreateAsync(new ApplicationRole { Name = userRoles[i] });

                            await userManager.CreateAsync(new ApplicationUser { UserName = $"user{i + 1}", Email = userEmails[i] }, "password");

                            var user = await userManager.FindByEmailAsync(userEmails[i]);

                            await userManager.AddToRoleAsync(user, userRoles[i]);

                            if (i != 2)
                            {
                                await applicationContext.ProjectUsers.AddRangeAsync(new ProjectUsers { User = user, Project = projects[0] }, new ProjectUsers { User = user, Project = projects[1] });
                            }
                            else
                            {
                                await applicationContext.ProjectUsers.AddAsync(new ProjectUsers { User = user, Project = projects[0] });
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                }
            }

            host.Run();
        }