public async Task<IHttpActionResult> PostCustomer(Customer customer) { if (!ModelState.IsValid) { return BadRequest(ModelState); } customer.TrackingState = TrackingState.Added; _dbContext.ApplyChanges(customer); try { await _dbContext.SaveChangesAsync(); } catch (DbUpdateException) { if (_dbContext.Customers.Any(c => c.CustomerId == customer.CustomerId)) { return Conflict(); } throw; } await _dbContext.LoadRelatedEntitiesAsync(customer); customer.AcceptChanges(); return CreatedAtRoute("DefaultApi", new { id = customer.CustomerId }, customer); }
public async Task<IHttpActionResult> PutCustomer(Customer customer) { if (!ModelState.IsValid) { return BadRequest(ModelState); } _dbContext.ApplyChanges(customer); try { await _dbContext.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!_dbContext.Customers.Any(c => c.CustomerId == customer.CustomerId)) { return Conflict(); } throw; } await _dbContext.LoadRelatedEntitiesAsync(customer); customer.AcceptChanges(); return Ok(customer); }