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

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var context     = services.GetRequiredService <ApplicationDbContext>();
                    var userManager = services.GetRequiredService <UserManager <ApplicationUser> >();
                    var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >();
                    await ContextSeed.SeedRolesAsync(userManager, roleManager);

                    await ContextSeed.SeedSuperAdminAsync(userManager, roleManager);
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occurred while seeding the database.");
                }
            }
            host.Run();
        }
Ejemplo n.º 2
0
        public async static Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var logger = loggerFactory.CreateLogger <Program>();

                    // Beging logging

                    logger?.LogInformation("Begin seeding roles into the database");


                    var context     = services.GetRequiredService <ApplicationDbContext>();
                    var userManager = services.GetRequiredService <UserManager <ApplicationUser> >();
                    var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >();
                    var config      = services.GetRequiredService <IConfiguration>();

                    // Seed the Roles into the database
                    await ContextSeed.SeedRoleAsync(roleManager);

                    //used User Secret.json to get the password
                    var pwd = config["SuperAdmin"];

                    // Seed the SuperAdmin Role to the database
                    await ContextSeed.SeedSuperAdminAsync(userManager, pwd);

                    logger?.LogInformation("Seeding roles into the database was successful");
                }
                catch (Exception ex)
                {
                    // Log any issuse while seeding occurs
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger?.LogError(ex.Message, "An error occurred while seeding the DB. Seeding failed!!");
                }
            }
            host.Run();
        }
Ejemplo n.º 3
0
 private static void seedAdmin(IHost host)
 {
     using (var scope = host.Services.CreateScope())
     {
         var services      = scope.ServiceProvider;
         var loggerFactory = services.GetRequiredService <ILoggerFactory>();
         try
         {
             var context     = services.GetRequiredService <DBContext>();
             var userManager = services.GetRequiredService <UserManager <ApplicationUser> >();
             var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >();
             ContextSeed.SeedRolesAsync(userManager, roleManager).Wait();
             ContextSeed.SeedSuperAdminAsync(userManager, roleManager).Wait();
         }
         catch (Exception ex)
         {
             var logger = loggerFactory.CreateLogger <Program>();
             logger.LogError(ex, "An error occurred seeding the DB.");
         }
     }
 }
Ejemplo n.º 4
0
        public static void Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            //Dependancy Injection to bring in all middleware services using in the app
            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                //Everything here surrounded in try catch incase something breaks
                try
                {
                    //context for the database
                    var context = services.GetRequiredService <ApplicationDbContext>();
                    //userMgr handles all the identityusers
                    var userManager = services.GetRequiredService <UserManager <ApplicationUser> >();
                    //roleMgr handles all the identityroles
                    var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >();
                    //check to make sure database is created, apply the latest migrations just in case.
                    context.Database.EnsureCreated();
                    //if no roles exist in db seed roles
                    if (!context.Roles.Any())
                    {
                        ContextSeed.SeedRolesAsync(userManager, roleManager).GetAwaiter().GetResult();
                    }
                    //if no users exist in db seed users and assign them to default roles
                    if (!context.Users.Any())
                    {
                        ContextSeed.SeedSuperAdminAsync(userManager, roleManager).GetAwaiter().GetResult();
                    }
                }
                catch (Exception e)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(e, "Error occurred seeding the db. Check program.cs");
                }
            }
            host.Run();
        }
Ejemplo n.º 5
0
        public async static Task Main(string[] args)
        {
            var host = CreateHostBuilder(args).Build();

            using (var scope = host.Services.CreateScope())
            {
                var services      = scope.ServiceProvider;
                var loggerFactory = services.GetRequiredService <ILoggerFactory>();
                try
                {
                    var context     = services.GetRequiredService <SystemContext>();
                    var userManager = services.GetRequiredService <UserManager <User> >();
                    var roleManager = services.GetRequiredService <RoleManager <IdentityRole> >();
                    await ContextSeed.SeedRolesAsync(userManager, roleManager);

                    await ContextSeed.SeedSuperAdminAsync(userManager, roleManager);

                    Status status = context.Statuses.FirstOrDefault(s => s.StatusName.ToLower().Equals("в обработке"));
                    if (status == null)
                    {
                        context.Statuses.Add(new Status()
                        {
                            StatusName = "В обработке"
                        });
                        context.SaveChanges();
                    }
                    status = context.Statuses.FirstOrDefault(s => s.StatusName.ToLower().Equals("отменена"));
                    if (status == null)
                    {
                        context.Statuses.Add(new Status()
                        {
                            StatusName = "Отменена"
                        });
                        context.SaveChanges();
                    }
                    status = context.Statuses.FirstOrDefault(s => s.StatusName.ToLower().Equals("в пользовании"));
                    if (status == null)
                    {
                        context.Statuses.Add(new Status()
                        {
                            StatusName = "В пользовании"
                        });
                        context.SaveChanges();
                    }
                    status = context.Statuses.FirstOrDefault(s => s.StatusName.ToLower().Equals("возвращено"));
                    if (status == null)
                    {
                        context.Statuses.Add(new Status()
                        {
                            StatusName = "возвращено"
                        });
                        context.SaveChanges();
                    }
                }
                catch (Exception ex)
                {
                    var logger = loggerFactory.CreateLogger <Program>();
                    logger.LogError(ex, "An error occurred seeding the DB.");
                }
            }
            host.Run();
        }