コード例 #1
0
        public async Task <IActionResult> PutProduct(long id, Product product)
        {
            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());
        }
コード例 #2
0
        public async Task <IActionResult> PutCustomer(long id, Customer customer)
        {
            if (id != customer.Id)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }
コード例 #3
0
 public virtual async Task <int> AddAsync(TEntity obj)
 {
     if (obj != null)
     {
         DbSet.Add(obj);
         return(await DbContext.SaveChangesAsync());
     }
     else
     {
         throw new ArgumentNullException();
     }
 }
コード例 #4
0
        public async Task <ActionResult <Customer> > Post(Customer newCustomer)
        {
            newCustomer.id = Guid.NewGuid();
            _context.Customers.Add(newCustomer);
            await _context.SaveChangesAsync();

            return(CreatedAtAction(nameof(GetAll), newCustomer));
        }
コード例 #5
0
        public async Task <IActionResult> PutOrder(long id, Order order)
        {
            var originalOrder = _context.Orders
                                .Where(o => o.Id == id)
                                .Include(o => o.Rows)
                                .SingleOrDefault();

            originalOrder.Rows.ToList().ForEach(r => _context.Entry(r).State = EntityState.Deleted);

            await _context.SaveChangesAsync();

            foreach (var orderRow in order.Rows)
            {
                orderRow.Id = 0;

                _context.OrderRows.Add(orderRow);
            }

            await _context.SaveChangesAsync();

            _context.Entry(originalOrder).CurrentValues.SetValues(order);

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

            return(NoContent());
        }
コード例 #6
0
 public async Task CommitAsync()
 {
     await _db.SaveChangesAsync();
 }