public override void Execute() { this.Data = new PetStoreEntities(); var categoriesIds = this.Data .Categories .OrderBy(c => Guid.NewGuid()) .Select(c => c.Id) .ToList(); var categoriesIndex = 0; for (int i = 0; i < MaxNumberOfProducts; i++) { if (categoriesIndex == 50) { categoriesIndex = 0; } var product = new Product(); product.Name = this.Generator.GetRandomString(5, 25); product.Price = this.Generator.GetRandomNumber(10, 10000); product.CategoryId = categoriesIds[categoriesIndex]; product.Species = this.GetSomeSpecies(2, 10); this.Data.Products.Add(product); if (i % 10 == 0) { Console.Write("."); } if (i % 100 == 0) { this.Data.SaveChanges(); this.Data.Dispose(); this.Data = new PetStoreEntities(); } categoriesIndex++; } this.Data.SaveChanges(); }
private void AddProduct(PetStoreEntities db, int categoryId, IList<Species> species) { var product = new Product { CategoryId = categoryId, Name = RandomGenerator.RandomString(5, 25), Price = RandomGenerator.RandomNumber(10, 1000) }; var numberOfSpeciesPerProduct = RandomGenerator.RandomNumber(2, 10); for (int i = 0; i < numberOfSpeciesPerProduct; i++) { product.Species.Add(species[RandomGenerator.RandomNumber(0, species.Count - 1)]); } db.Products.Add(product); }