public async Task SeedAsync(EClinicDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext == null)
            {
                throw new ArgumentNullException(nameof(dbContext));
            }

            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            var logger = serviceProvider.GetService <ILoggerFactory>().CreateLogger(typeof(ApplicationDbContextSeeder));

            var seeders = new List <ISeeder>
            {
                new RolesSeeder(),
                new SettingsSeeder(),
            };

            foreach (var seeder in seeders)
            {
                await seeder.SeedAsync(dbContext, serviceProvider);

                await dbContext.SaveChangesAsync();

                logger.LogInformation($"Seeder {seeder.GetType().Name} done.");
            }
        }
Example #2
0
        public async Task SeedAsync(EClinicDbContext dbContext, IServiceProvider serviceProvider)
        {
            if (dbContext == null)
            {
                throw new ArgumentNullException(nameof(dbContext));
            }

            if (serviceProvider == null)
            {
                throw new ArgumentNullException(nameof(serviceProvider));
            }

            var logger = serviceProvider.GetService <ILoggerFactory>().CreateLogger(typeof(SiteDbContextSeeder));

            var userStore = serviceProvider.GetService <UserManager <EClinicUser> >();

            var seeders = new List <ISeeder>
            {
                new SiteSeeder(),
            };

            foreach (var seeder in seeders)
            {
                await seeder.SeedAsync(dbContext, serviceProvider);

                await dbContext.SaveChangesAsync();

                logger.LogInformation($"Seeder {seeder.GetType().Name} done.");
            }

            var AdminUserToAdd = new EClinicUser
            {
                Email              = "*****@*****.**",
                FirstName          = "Ivo",
                MiddleName         = "Peshov",
                LastName           = "Petrov",
                UserName           = "******",
                NormalizedEmail    = "*****@*****.**",
                NormalizedUserName = "******",
                Address            = "Nqkyde Tam 35",
                Age           = 25,
                SecurityStamp = Guid.NewGuid().ToString(),
                CreatedOn     = DateTime.UtcNow,
            };

            var userToAdd = new EClinicUser
            {
                Email              = "*****@*****.**",
                FirstName          = "Petyr",
                MiddleName         = "Peshov",
                LastName           = "Petrov",
                UserName           = "******",
                NormalizedEmail    = "*****@*****.**",
                NormalizedUserName = "******",
                Address            = "I tuk i tam",
                Age           = 30,
                SecurityStamp = Guid.NewGuid().ToString(),
                CreatedOn     = DateTime.UtcNow,
            };


            await userStore.CreateAsync(AdminUserToAdd, "123456");

            await userStore.CreateAsync(userToAdd, "123456");

            dbContext.SaveChanges();
            var adminUser = await userStore.FindByEmailAsync("*****@*****.**");

            var user = await userStore.FindByEmailAsync("*****@*****.**");

            await userStore.AddToRoleAsync(adminUser, GlobalConstants.AdministratorRoleName);

            await userStore.AddToRoleAsync(user, GlobalConstants.UserRoleName);
        }