protected override void GenerateEntity()
        {
            var toy = new Toy
                          {
                              Name = this.Random.GetString(5, 45), 
                              Type = this.Random.GetString(5, 45), 
                              Price = (decimal)(this.Random.GetDouble() * 100), 
                              Color = this.Random.GetChance(50) ? this.Random.GetString(5, 30) : null, 
                              ManufacturerId = this.manufacturers[this.Random.GetInt(0, this.manufacturers.Count - 1)], 
                              AgeRangesId = this.ageRangesIds[this.Random.GetInt(0, this.ageRangesIds.Count - 1)], 
                          };

            if (this.categoryIds.Count > 0)
            {
                var uniqueCategoriesId = new HashSet<int>();
                var categoriesInToy = this.Random.GetInt(1, Math.Min(10, this.categoryIds.Count - 1));

                while (uniqueCategoriesId.Count != categoriesInToy)
                {
                    uniqueCategoriesId.Add(this.categoryIds[this.Random.GetInt(0, this.categoryIds.Count - 1)]);
                }

                foreach (var categoryId in uniqueCategoriesId)
                {
                    toy.Categories.Add(this.databaseContext.Categories.Find(categoryId));
                }
            }

            this.databaseContext.Toys.Add(toy);
        }
示例#2
0
        public override void Generate()
        {
            var ageRangeIds = this.db.AgeRanges.Select(a => a.Id).ToArray();
            var manufacturerIds = this.db.Manufacturers.Select(m => m.Id).ToArray();
            var categoryIds = this.db.Categories.Select(c => c.Id).ToArray();

            Console.WriteLine("Adding toys");

            for (int i = 0; i < this.count; i++)
            {
                var toy = new Toy
                {
                    Name=random.GetRandomStringRandomLength(5, 20),
                    Type = random.GetRandomStringRandomLength(5, 20),
                    Color = random.GetRandomStringRandomLength(5, 20),
                    Price=random.GetRandomNumber(2, 1000),
                    ManufacturerId = manufacturerIds[random.GetRandomNumber(0, manufacturerIds.GetLength(0))],
                    AgeRangeId = ageRangeIds[random.GetRandomNumber(0, ageRangeIds.GetLength(0))],
                };

                var uniqueCategoryIds = new HashSet<int>();

                if (categoryIds.GetLength(0) > 0)
                {
                    var categoriesCountInToy = this.random.GetRandomNumber(1, Math.Max(10, categoryIds.GetLength(0)));

                    while (uniqueCategoryIds.Count != categoriesCountInToy)
                    {
                        uniqueCategoryIds.Add(categoryIds[this.random.GetRandomNumber(0, categoryIds.GetLength(0))]);
                    }
                }

                foreach (var categoryId in uniqueCategoryIds)
                {
                    toy.Categories.Add(this.db.Categories.Find(categoryId));
                }

                db.Toys.Add(toy);

                if (i % 100 == 0)
                {
                    db.SaveChanges();
                    //db.SaveChangesAsync();
                    db = new ToyStoreEntities();
                    Console.Write(".");
                }
            }

            Console.WriteLine("Toys added");
        }
示例#3
0
        public override void Generate()
        {
            Console.WriteLine("Generating Toys");

            var ageRangesIds = this.Db.AgeRanges.Select(a => a.AgeRangeId).ToList();
            var manufacturersIds = this.Db.Manufacturers.Select(m => m.ManufacturerID).ToList();
            var categoriesIds = this.Db.Categories.Select(c => c.CategoryID).ToList();

            for (int i = 0; i < this.Count; i++)
            {
                var newToy = new Toy
                {
                    Name = this.Random.GetString(2, 50),
                    Price = (decimal)Math.Round(this.Random.GetDouble(0.1, 100), 2),
                    Type = this.Random.GetChance(90) ? this.Random.GetString(2, 50) : null,
                    Color = this.Random.GetChance(90) ? this.Random.GetString(2, 50) : null,
                    ManufacturerID = manufacturersIds[this.Random.GetInt(0, manufacturersIds.Count - 1)],
                    AgeRangeID = ageRangesIds[this.Random.GetInt(0, manufacturersIds.Count - 1)]
                };
                if (categoriesIds.Count > 0)
                {
                    var uniqueCategoriesIds = new HashSet<int>();
                    var categoriesInToy = this.Random.GetInt(1, Math.Min(categoriesIds.Count - 1, 10));
                    while (uniqueCategoriesIds.Count != categoriesInToy)
                    {
                        uniqueCategoriesIds.Add(categoriesIds[this.Random.GetInt(0, categoriesIds.Count - 1)]);
                    }
                    foreach (var uniqueCategorieId in uniqueCategoriesIds)
                    {
                        newToy.Categories.Add(this.Db.Categories.Find(uniqueCategorieId));
                    }
                }

                this.Db.Toys.Add(newToy);
                if (i % 100 == 0)
                {
                    Console.Write(".");
                    Db.SaveChanges();
                }
            }
            Console.WriteLine("\nGenerating Toys Done!");
        }
        public Toy GetRandomToy(IList<int> colorIds, IList<int> manufacturerIds, IList<int> ageRangeIds)
        {
            var rndColorId = colorIds[generator.GetRandomInt(0, colorIds.Count - 1)];
            var rndManufacturerId = manufacturerIds[generator.GetRandomInt(0, manufacturerIds.Count - 1)];
            var rndAgeRangeId = ageRangeIds[generator.GetRandomInt(0, ageRangeIds.Count - 1)];

            var toy = new Toy()
            {
                Name = generator.GetRandomString(5, 20, true),
                Price = generator.GetRandomDecimal(1, 500),
                Type = generator.GetRandomString(2, 10),
                ColorId = rndColorId,
                ManufacturerId = rndManufacturerId,
                AgeRangeId = rndAgeRangeId
            };

            return toy;
        }