public IHttpActionResult PostProduct(Product product)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            db.Product.Add(product);
            db.SaveChanges();

            return CreatedAtRoute("DefaultApi", new { id = product.ProductId }, product);
        }
Exemplo n.º 2
0
        public int AddProduct()
        {
            var product = new Product
            {
                ProductName = "Jacket",
                Price = 999,
                Active = true,
                Stock = 10
            };
            db.Product.Add(product);

            db.SaveChanges();

            return product.ProductId;

            return 0;
        }
        // GET: EF
        public int AddProduct()
        {
            using (var db = new FabricsEntities())
            {
                Product product = new Product();

                product = new Product()
                {
                    ProductName = "Tee white",
                    Active = true,
                    Stock = 10,
                    Price = 299
                };
                db.Product.Add(product);
                db.SaveChanges();

                return product.ProductId;
            }
        }
        public IHttpActionResult PutProduct(int id, Product product)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            if (id != product.ProductId)
            {
                return BadRequest();
            }

            db.Entry(product).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }