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