private static void SeedCategoryProducts() { var context = new ProductShopContext(); var productKeys = context.Products.Select(x => x.Id).ToArray(); var categoryKeys = context.Categories.Select(x => x.Id).ToArray(); var random = new Random(); var productCategories = new List <CategoryProduct>(); for (int i = 0; i < productKeys.Length; i++) { var productCategory = new CategoryProduct() { ProductId = productKeys[i], CategoryId = categoryKeys[random.Next(0, categoryKeys.Length)] }; productCategories.Add(productCategory); } context.CategoryProducts.AddRange(productCategories); context.SaveChanges(); context.Dispose(); }