// POST api/Orders public HttpResponseMessage PostOrder(Order order) { if (!ModelState.IsValid) { return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState)); } order.TrackingState = TrackingState.Added; _dbContext.ApplyChanges(order); try { _dbContext.SaveChanges(); } catch (DbUpdateException ex) { if (_dbContext.Orders.Any(o => o.OrderId == order.OrderId)) { return(Request.CreateErrorResponse(HttpStatusCode.Conflict, ex)); } throw; } _dbContext.LoadRelatedEntities(order); order.AcceptChanges(); HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, order); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = order.OrderId })); return(response); }