コード例 #1
0
 private static void SeedSubCategory(AuctionSystemDbContext context)
 {
     context.SubCategories.AddAsync(new SubCategory {
         Id = DataConstants.SampleSubCategoryId
     });
     context.SaveChangesAsync();
 }
コード例 #2
0
        private static async Task SeedItems(AuctionSystemDbContext dbContext)
        {
            if (!dbContext.Items.Any())
            {
                var random   = new Random();
                var allItems = new List <Item>();
                foreach (var category in dbContext.Categories.Include(c => c.SubCategories))
                {
                    int i = 1;
                    foreach (var subCategory in category.SubCategories)
                    {
                        var startTime = DateTime.UtcNow.AddDays(random.Next(0, 5));
                        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,
                            UserId        = dbContext.Users.First().Id
                        };

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

                await dbContext.Items.AddRangeAsync(allItems);

                await dbContext.SaveChangesAsync();
            }
        }
コード例 #3
0
 private static void SeedPictures(AuctionSystemDbContext context)
 {
     context.Pictures.AddAsync(new Picture
     {
         Id = DataConstants.SamplePictureId, ItemId = DataConstants.SampleItemId
     });
     context.SaveChangesAsync(CancellationToken.None);
 }
コード例 #4
0
        private static async Task SeedCategories(AuctionSystemDbContext dbContext)
        {
            if (!dbContext.Categories.Any())
            {
                var categories = File.ReadAllText(WebConstants.CategoriesPath);

                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.AddRangeAsync(allCategories);

                await dbContext.SaveChangesAsync();
            }
        }