public async Task SeedAsync(CinemaWorldDbContext 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 DirectorsSeeder(), new SeatsSeeder(new Random()), }; foreach (var seeder in seeders) { await seeder.SeedAsync(dbContext, serviceProvider); await dbContext.SaveChangesAsync(); logger.LogInformation($"Seeder {seeder.GetType().Name} done."); } }
public async Task SeedAsync(CinemaWorldDbContext dbContext, IServiceProvider serviceProvider) { // Custom seed halls with requested amount of seats if (!dbContext.Seats.Any()) { return; } var seats = new List <Seat>(); for (int row = 1; row <= 10; row++) { for (int col = 1; col <= 10; col++) { int category = this.random.Next((int)SeatCategory.Normal, (int)SeatCategory.Wheelchair + 1); var seat = new Seat { RowNumber = row, Number = col, HallId = 7, Category = (SeatCategory)category, }; seats.Add(seat); } } foreach (var seat in seats) { await dbContext.Seats.AddAsync(new Seat { RowNumber = seat.RowNumber, Number = seat.Number, IsAvailable = true, HallId = seat.HallId, Category = seat.Category, }); } await dbContext.SaveChangesAsync(); }