コード例 #1
0
 public IActionResult CreateProduct(Product p)
 {
     if (ModelState.IsValid)
     {
         context.Create(p);
     }
     return(Redirect("/"));
 }
コード例 #2
0
ファイル: Program.cs プロジェクト: iiliev92/CodeFirstCoreAPI
        static void CreateProduct()
        {
            Product product = new Product();

            product.Barcode = Guid.NewGuid().ToString();
            product.Name    = Guid.NewGuid().ToString();

            Random r = new Random();

            product.Price    = r.Next();
            product.Quantity = r.Next();

            BrandContext brandContext = new BrandContext(new Context());
            List <Brand> brands       = (List <Brand>)brandContext.ReadAll();

            product.Brand = brands[r.Next(0, brands.Count)];

            ProductContext productContext = new ProductContext(new Context());

            productContext.Create(product);

            Console.WriteLine("Product created successfully");
        }