コード例 #1
0
        static void AddProduct(string categoryName, string productName, double price)
        {
            using var shoppingContext = new ShoppingContext();

            var category = shoppingContext.Category.Where(x => x.Name == categoryName).FirstOrDefault();

            var product = new Product
            {
                Name       = productName,
                Price      = price,
                Categories = new List <ProductCategory>()
                {
                    new ProductCategory()
                    {
                        Category = category
                    }
                }
            };

            shoppingContext.Products.Add(product);
            shoppingContext.SaveChanges();
        }