Beispiel #1
0
        private static async Task SeedInDb(ElectricTravelDbContext dbContext)
        {
            dbContext.Regions.Add(new Region
            {
                Name = "Plovdiv Region",
            });

            await dbContext.SaveChangesAsync();

            var region = await dbContext.Regions.FirstOrDefaultAsync(x => x.Name == "Plovdiv Region");

            dbContext.Cities.Add(new City
            {
                Name   = "Plovdiv",
                Region = region,
            });

            await dbContext.SaveChangesAsync();
        }
        public async Task GetCountShouldReturnCorrectNumberUsingDbContext()
        {
            var options = new DbContextOptionsBuilder <ElectricTravelDbContext>()
                          .UseInMemoryDatabase(databaseName: "SettingsTestDb").Options;

            using var dbContext = new ElectricTravelDbContext(options);
            dbContext.Settings.Add(new Setting());
            dbContext.Settings.Add(new Setting());
            dbContext.Settings.Add(new Setting());
            await dbContext.SaveChangesAsync();

            using var repository = new EfDeletableEntityRepository <Setting>(dbContext);
            var service = new SettingsService(repository);

            Assert.Equal(3, service.GetCount());
        }
Beispiel #3
0
        public async Task SeedAsync(ElectricTravelDbContext 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(),
                new CurrencySeeder(),
                new CarAdvertStatusSeeder(),
                new TypeTravelSeeder(),
                new ImageTypeSeeder(),
                new SharedTravelStatusSeeder(),
                new RegionSeeder(),
                new CitiesSeeder(),
                new CarTypesSeeder(),
                new CarMakesSeeder(),
                new CarModelsSeeder(),
                new AdminsSeeder(),
            };

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

                await dbContext.SaveChangesAsync();

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