Example #1
0
        public static void ImportCountries(int count)
        {
            Console.WriteLine("ImportCountries");
            var countriesNames = new HashSet<string>();

            do
            {
                countriesNames.Add(RandomGenerator.RandomString(5, 50));
            }
            while (countriesNames.Count < count);

            var data = new PetStoreEntities();
            var counter = 0;
            foreach (var coutry in countriesNames)
            {
                var newCountry = new Country()
                {
                    Name = coutry
                };

                data.Countries.Add(newCountry);
                counter++;

                if (counter % 100 == 0)
                {
                    data.SaveChanges();
                    data.Dispose();
                    data = new PetStoreEntities();
                    Console.Write(".");
                }
            }

            data.SaveChanges();
            data.Dispose();
        }
        private static void GenerateCategories(PetStoreEntities data)
        {
            List<Category> addedCategories = new List<Category>();
            const int numberOfCategoriesToAdd = 50;

            Console.Write("Adding categories:");
            for (int i = 0; i < numberOfCategoriesToAdd; i++)
            {
                addedCategories.Add(new Category {
                    Name = randomGenerator.GetRandomString(randomGenerator.GetRandomNumber(5, 20))
                });
                Console.Write(".");
            }

            Console.WriteLine();

            data.Categories.AddRange(addedCategories);
            data.SaveChanges();
            Console.WriteLine("Categories added");
        }
        private static void GenerateCountries(PetStoreEntities data)
        {
            const int numberOfCountriesToGenerate = 20;
            List<Country> addedCountries = new List<Country>();

            Console.Write("Adding countries:");
            for (int i = 0; i < numberOfCountriesToGenerate; i++)
            {
                addedCountries.Add(new Country
                {
                    Name = randomGenerator.GetRandomString(randomGenerator.GetRandomNumber(5, 50))
                });

                Console.Write(".");
            }
            Console.WriteLine();

            data.Countries.AddRange(addedCountries);
            data.SaveChanges();
            Console.WriteLine("Countries added");
        }
        private static void GeneratePets(PetStoreEntities data)
        {
            List<Pet> addedPets = new List<Pet>();
            var species = data.Species.Select(s => s.Id).ToList();
            var colors = data.Colors.Select(c => c.Id).OrderBy(c => c).ToList() ;

            Console.Write("Adding pets:");
            foreach (var specie in species)
            {
                var numberOfPetsToAdd = randomGenerator.GetRandomNumber(40, 60);
                for (int i = 0; i < numberOfPetsToAdd; i++)
                {
                    addedPets.Add(new Pet
                    {
                        SpeciesId = specie,
                        Price = randomGenerator.GetRandomNumber(5, 2500),
                        BirthDateTime = randomGenerator.GetRandomDate(new DateTime(2010, 1, 1), DateTime.Now.AddDays(-60)),
                        ColorId = randomGenerator.GetRandomNumber(colors.First(), colors.Last())
                    });
                }

                Console.Write(".");
            }
            Console.WriteLine();

            data.Pets.AddRange(addedPets);
            data.SaveChanges();
            Console.WriteLine("Pets added");
        }
        private static void GenerateSpecies(PetStoreEntities data)
        {
            List<Species> addedSpecies = new List<Species>();
            var countriesIds = data.Countries.Select(c => c.Id).ToList();

            Console.Write("Adding species:");

            foreach (var country in countriesIds)
            {
                int numberOfSpeciesToAdd = randomGenerator.GetRandomNumber(1, 9);

                for (int i = 0; i < numberOfSpeciesToAdd; i++)
                {
                    addedSpecies.Add(new Species
                    {
                        Name = randomGenerator.GetRandomString(randomGenerator.GetRandomNumber(5,50)),
                        CountryId = country
                    });

                    Console.Write(".");
                }
            }

            Console.WriteLine();

            data.Species.AddRange(addedSpecies);
            data.SaveChanges();
            Console.WriteLine("Species added");
        }
        private static void GenerateProducts(PetStoreEntities data)
        {
            var categories = data.Categories.Select(c => c.Id).ToList();
            var species = data.Species.ToList();
            List<Product> addedProducts = new List<Product>();

            const int minSpeciesCount = 2;
            const int maxSpeciesCount = 10;

            Console.Write("Adding products:");
            foreach (var category in categories)
            {
                int numberOfProductsToAdd = randomGenerator.GetRandomNumber(350, 450);

                for (int i = 0; i < numberOfProductsToAdd; i++)
                {
                    addedProducts.Add(new Product
                    {
                        Name = randomGenerator.GetRandomString(randomGenerator.GetRandomNumber(5,25)),
                        CategoryId = category,
                        Price = randomGenerator.GetRandomNumber(10,1000),
                        Species = species
                            .OrderBy(s => Guid.NewGuid())
                            .Take(randomGenerator.GetRandomNumber(minSpeciesCount,maxSpeciesCount))
                            .ToList()
                    });

                    Console.Write(".");
                }
            }

            Console.WriteLine();

            data.Products.AddRange(addedProducts);
            data.SaveChanges();
            Console.WriteLine("Products added");
        }
Example #7
0
        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();
        }
Example #8
0
        public static void ImportSpecies(int count)
        {
            Console.WriteLine("ImportSpecies");
            var data = new PetStoreEntities();

            var coutriesIds = data.Countries.Select(c => c.Id).ToList();
            var countriesCount = coutriesIds.Count;

            // generate 100 ids list
            var countriesRandomCountryIds = new List<int>();

            // add the unique 20
            countriesRandomCountryIds.AddRange(coutriesIds);

            // add the rest to count
            for (int i = 0; i < count - countriesCount; i++)
            {
                var randomCountryIndex = RandomInstance.Next(0, countriesCount);
                var randomId = coutriesIds[randomCountryIndex];
                countriesRandomCountryIds.Add(randomId);
            }

            // randomize
            var shuffledCountryIds = countriesRandomCountryIds.OrderBy(g => Guid.NewGuid()).ToList();

            var speciesNames = new HashSet<string>();
            do
            {
                speciesNames.Add(RandomGenerator.RandomString(5, 50));
            }
            while (speciesNames.Count < count);

            var counter = 0;
            foreach (var specie in speciesNames)
            {
                var id = shuffledCountryIds[0];
                shuffledCountryIds.RemoveAt(0);
                var newSpecie = new Species()
                {
                    Name = specie,
                    CountryId = id
                };

                data.Species.Add(newSpecie);
                counter++;

                if (counter % 100 == 0)
                {
                    data.SaveChanges();
                    data.Dispose();
                    data = new PetStoreEntities();
                    Console.Write(".");
                }
            }

            data.SaveChanges();
            data.Dispose();
        }
Example #9
0
        public static void ImportProducts(int count)
        {
            Console.WriteLine("ImportProducts");
            var data = new PetStoreEntities();

            var categoriesIds = data.Categories.Select(c => c.Id).ToList();
            var categoriesCount = categoriesIds.Count;

            // generate 20000 ids list
            var categoriesRandomCountryIds = new List<int>();

            // add the unique 50
            categoriesRandomCountryIds.AddRange(categoriesIds);

            // add the rest to count
            for (int i = 0; i < count - categoriesCount; i++)
            {
                var randomCategoriesIndex = RandomInstance.Next(0, categoriesCount);
                var randomId = categoriesIds[randomCategoriesIndex];
                categoriesRandomCountryIds.Add(randomId);
            }

            // randomize
            var shuffledCategoryIds = categoriesRandomCountryIds.OrderBy(g => Guid.NewGuid()).ToList();

            var productsNames = new HashSet<string>();
            do
            {
                productsNames.Add(RandomGenerator.RandomString(5, 25));
            }
            while (productsNames.Count < count);
            var counter = 0;
            foreach (var product in productsNames)
            {
                var id = shuffledCategoryIds[0];
                shuffledCategoryIds.RemoveAt(0);
                var newProduct = new PetProduct()
                {
                    Name = product,
                    CategoryId = id,
                    Price = (decimal)((RandomInstance.NextDouble() * (1000 - 10)) + 10)
                };

                data.PetProducts.Add(newProduct);
                counter++;

                // add species match here
                var speciesCount = RandomInstance.Next(2, 11);
                var speciesToAdd = new HashSet<Species>();
                var species = data.Species.OrderBy(s => Guid.NewGuid()).Take(speciesCount).ToList();
                while (speciesToAdd.Count != speciesCount)
                {
                    var specieIdRandomIndex = RandomInstance.Next(0, species.Count);
                    var randomSpecie = species[specieIdRandomIndex];
                    speciesToAdd.Add(randomSpecie);
                }

                foreach (var specie in speciesToAdd)
                {
                    newProduct.Species.Add(specie);
                }

                if (counter % 10 == 0)
                {
                    data.SaveChanges();
                    data.Dispose();
                    data = new PetStoreEntities();
                    Console.Write(".");
                }
            }

            data.SaveChanges();
            data.Dispose();
        }