private static void SeedProducts(ChameleonStoreContext context, ProductDto[] productDtos) { var productsToCreate = new List <Product>(); foreach (var productDto in productDtos) { var category = context.Categories.SingleOrDefault(x => x.Name == productDto.CategoryName); var brand = context.Brands.SingleOrDefault(x => x.Name == productDto.BrandName); if (category != null) { var product = new Product { Name = productDto.Name, ImageURL = productDto.ImageURL, Price = productDto.Price, Discount = productDto.Discount, Condition = productDto.Condition, Description = productDto.Description, BrandId = brand.Id, CategoryId = category.Id, Views = productDto.Views }; productsToCreate.Add(product); } } context.Products.AddRange(productsToCreate); context.SaveChanges(); }
private static void SeedBrands(ChameleonStoreContext context, BrandDto[] brandDtos) { var brandsToCreate = brandDtos .Select(c => new Brand { Name = c.Name }) .ToArray(); context.Brands.AddRange(brandsToCreate); context.SaveChanges(); }
private static void SeedCategories(ChameleonStoreContext context, CategoryDto[] categoryDtos) { var categoriesToCreate = categoryDtos .Select(c => new Category { Name = c.Name }) .ToArray(); context.Categories.AddRange(categoriesToCreate); context.SaveChanges(); }