public async Task <ActionResult <Product> > PostProduct(Product product)
        {
            _context.Entry(product).State = EntityState.Added;
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetProduct), new { id = product.ProductId }, product));
        }
예제 #2
0
        public async Task <IActionResult> PostProduct([FromBody] Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.Entry(product).State = EntityState.Added;
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetProduct", new { id = product.ProductId }, product));
        }
예제 #3
0
        public async Task <IActionResult> Post([FromBody] Product item)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.Entry(item).State = EntityState.Added;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                if (ItemExists(item.ProductId))
                {
                    return(StatusCode((int)HttpStatusCode.Conflict));
                }
                throw;
            }

            return(Created(item));
        }
예제 #4
0
        public void ApplyChanges_Should_Mark_Entity_As_Added()
        {
            using (var context = new NorthwindSlimContext(_options))
            {
                // Arrange
                var product = new Product
                {
                    ProductId   = 1,
                    ProductName = "Chai",
                    UnitPrice   = 10
                };
                product.TrackingState = TrackingState.Added;

                // Act
                context.ApplyChanges(product);

                // Assert
                Assert.Equal(EntityState.Added, context.Entry(product).State);
            }
        }