示例#1
0
        private static async Task SeedCategories(IAuctionSystemDbContext dbContext, CancellationToken cancellationToken)
        {
            if (!dbContext.Categories.Any())
            {
                var categories =
                    await File.ReadAllTextAsync(Path.GetFullPath(AppConstants.CategoriesPath), cancellationToken);

                var deserializedCategoriesWithSubCategories =
                    JsonConvert.DeserializeObject <CategoryDto[]>(categories);

                var allCategories = deserializedCategoriesWithSubCategories.Select(deserializedCategory => new Category
                {
                    Name          = deserializedCategory.Name,
                    SubCategories = deserializedCategory.SubCategoryNames.Select(deserializedSubCategory =>
                                                                                 new SubCategory
                    {
                        Name = deserializedSubCategory.Name
                    }).ToList()
                }).ToList();

                await dbContext.Categories.AddRangeAsync(allCategories, cancellationToken);

                await dbContext.SaveChangesAsync(cancellationToken);
            }
        }
示例#2
0
        private async Task SeedItems(IAuctionSystemDbContext dbContext,
                                     IUserManager manager,
                                     CancellationToken cancellationToken)
        {
            if (!dbContext.Items.Any())
            {
                var random   = new Random();
                var allItems = new List <Item>();
                foreach (var category in dbContext.Categories.Include(c => c.SubCategories))
                {
                    var i = 1;
                    foreach (var subCategory in category.SubCategories)
                    {
                        var startTime = this.dateTime.UtcNow.AddDays(random.Next(0, 5)).ToUniversalTime();
                        var item      = new Item
                        {
                            Description   = $"Test Description_{i}",
                            Title         = $"Test Title_{i}",
                            StartTime     = startTime,
                            EndTime       = startTime.AddHours(random.Next(1, 10)),
                            StartingPrice = random.Next(10, 500),
                            MinIncrease   = random.Next(1, 100),
                            SubCategoryId = subCategory.Id,
                            Pictures      = new List <Picture>
                            {
                                new Picture {
                                    Url = AppConstants.DefaultPictureUrl, Created = this.dateTime.UtcNow
                                }
                            },
                            UserId = await manager.GetFirstUserId()
                        };

                        i++;
                        allItems.Add(item);
                    }
                }

                await dbContext.Items.AddRangeAsync(allItems, cancellationToken);

                await dbContext.SaveChangesAsync(cancellationToken);
            }
        }