Beispiel #1
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();
        }
        public static void Main(string[] args)
        {
            //CreateWebHostBuilder(args).Build().Run();

            var host = CreateWebHostBuilder(args).Build();

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

                try
                {
                    var context = services.GetRequiredService <kdubois1_MVC_MusicContext>();
                    context.Database.Migrate();
                    MUSeedData.Initialize(services);

                    var identityContext = services.GetRequiredService <ApplicationDbContext>();
                    ApplicationSeedData.SeedAsync(identityContext, services).Wait();
                }
                catch (Exception ex)
                {
                    var logger = services.GetRequiredService <ILogger <Program> >();
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
            }

            host.Run();
        }
Beispiel #3
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();
        }