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