Beispiel #1
0
        public async Task <IActionResult> PutProduct([FromRoute] int id, [FromBody] Product product)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

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

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

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ProductExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Beispiel #2
0
        public async Task <Product> UpdateAsync(Product entity)
        {
            var local = dbContext.Set <Product>()
                        .Local
                        .FirstOrDefault(entry => entry.Id.Equals(entity.Id));

            // check if local is not null
            if (local != null)
            {
                // detach
                dbContext.Entry(local).State = EntityState.Detached;
            }
            this.dbContext.Set <Product>().Update(entity);
            await this.dbContext.SaveChangesAsync();

            return(entity);
        }