public static void ImportPets(int count) { Console.WriteLine("ImportPets"); var data = new PetStoreEntities(); var speciesIds = data.Species.Select(c => c.Id).ToList(); var speciesCount = speciesIds.Count; // generate 5000 ids list var speciesRandomIds = new List<int>(); // add the unique speciesRandomIds.AddRange(speciesIds); // add the rest to count for (int i = 0; i < count - speciesCount; i++) { var randomSpecieIndex = RandomInstance.Next(0, speciesCount); var randomId = speciesIds[randomSpecieIndex]; speciesRandomIds.Add(randomId); } // randomize var shuffledSpeciesIds = speciesRandomIds.OrderBy(g => Guid.NewGuid()).ToList(); var colorsId = data.Colors.Select(c => c.Id).ToList(); var colorsCount = colorsId.Count; var counter = 0; foreach (var specieId in shuffledSpeciesIds) { var randomPrice = (decimal)((RandomInstance.NextDouble() * (2500 - 5)) + 5); var randomColorIndex = RandomInstance.Next(0, colorsCount); var randomColorId = colorsId[randomColorIndex]; var randomDate = RandomGenerator.RandomDate(new DateTime(2010, 1, 1), DateTime.Now.AddDays(-60)); var randomBreed = RandomGenerator.RandomString(0, 30); var newPet = new Pet() { SpecieId = specieId, DateOfBirth = randomDate, Price = randomPrice, ColorId = randomColorId, Breed = randomBreed }; data.Pets.Add(newPet); counter++; if (counter % 100 == 0) { data.SaveChanges(); data.Dispose(); data = new PetStoreEntities(); Console.Write("."); } } data.SaveChanges(); data.Dispose(); }
public override void Execute() { if (!this.Data.Colors.Any()) { this.AddDefaultColors(); } var colors = this.Data .Colors .OrderBy(c => Guid.NewGuid()) .Select(c => c.Id) .ToList(); var speciesIds = this.Data .Species .OrderBy(s => Guid.NewGuid()) .Select(s => s.Id) .ToList(); var specieIndex = 0; for (int i = 0; i < MaxNumberOfPets; i++) { if (specieIndex == 100) { specieIndex = 0; } var pet = new Pet() { Birth = this.Generator.GetRandomDate(before: DateTime.Now.AddDays(-60), after: new DateTime(2010, 1, 1)), Price = this.Generator.GetRandomNumber(5, 2500), ColorId = colors[this.Generator.GetRandomNumber(0, colors.Count - 1)], SpecieId = speciesIds[specieIndex] }; if (i % 10 == 0) { pet.Breed = this.Generator.GetRandomString(5, 30); } // set random breed for fun this.Data.Pets.Add(pet); // print . if (i % 10 == 0) { Console.Write("."); } // save dispose if (i % 100 == 0) { this.Data.SaveChanges(); this.Data.Dispose(); this.Data = new PetStoreEntities(); } specieIndex++; } this.Data.SaveChanges(); this.Data.Dispose(); this.Data = new PetStoreEntities(); }