title: OData V4 client adding child entity
body: <p>I have a parent (Order) and child (OrderDetail) where Order already exists in the database and OrderDetail also exists in the database.<br>
  All I really want to do is add another OrderDetail record bound to the Order.  </p>
  
  <p>I have been down several paths and I'm not even sure what is the correct path.<br>
  Let's make some assumptions that navigations between these are already working.<br>
  I can $expand=OrderDetails fine and I can also Orders(1)/OrderDetails fine and do the reverse from OrderDetails.  </p>
  
  <p>Based on this <a href="https://msdn.microsoft.com/en-us/library/dd756361(v=vs.110).aspx" rel="nofollow"><strong>Updating the Data Service</strong></a>, all I need to do is call AddRelatedObject and then Add the object to the OrderDetails collection.  </p>
  
  <pre><code>// Add the new item with a link to the related Order
  context.AddRelatedObject(order, "OrderDetails", newOrderDetail);
  // Add the new order detail to the collection
  order.Order_Details.Add(newOrderDetail);
  newOrderDetail.Order = order;
  </code></pre>
  
  <p>Seems simple enough.<br>
  Yet when I execute context.SaveChanges(SaveChangesOptions.ReplaceOnUpdate) it will throw an error.  </p>
  
  <blockquote>
    <p>{"error":{"code":"","message":"No HTTP resource was found that matches the request URI '<a href="http://localhost/Test/odata/Orders(1)/OrderDetails" rel="nofollow">http://localhost/Test/odata/Orders(1)/OrderDetails</a>'.","innererror":{"message":"No routing convention was found to select an action for the OData path with template '~/entityset/key/navigation'.","type":"","stacktrace":""}}}</p>
  </blockquote>
  
  <p>But if I navigate to the URL listed, it shows data.<br>
  Time for Fiddler.<br>
  In Fiddler, I can see this is a POST to the URL and not a GET.<br>
  Which it should be a POST but not to the URL listed.<br>
  The POST should have been to /odata/OrderDetails  </p>
  
  <p><strong>Round 2</strong>  </p>
  
  <pre><code>// Add the new item with a link to the related Order
  context.AttachTo("OrderDetails", newOrderDetail);
  // Add a link between Order and the new OrderDetail
  context.AddLink(order, "OrderDetails", newOrderDetail);
  // Add the new order detail to the collection
  order.Order_Details.Add(newOrderDetail);
  newOrderDetail.Order = order;
  </code></pre>
  
  <p>Still a POST with an error but the URL is slightly different and the json posted only has "/odata/OrderDetail(0)" it also now has "$ref".</p>
  
  <blockquote>
    <p>{"error":{"code":"","message":"No HTTP resource was found that matches the request URI '<a href="http://localhost/Test/odata/Orders(1)/OrderDetails/" rel="nofollow">http://localhost/Test/odata/Orders(1)/OrderDetails/</a>$ref'.","innererror":{"message":"No routing convention was found to select an action for the OData path with template '~/entityset/key/navigation/$ref'.","type":"","stacktrace":""}}}</p>
  </blockquote>
  
  <p>Well a quick web search led me to this article <a href="https://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/entity-relations-in-odata-v4" rel="nofollow"><strong>Entity Relations in OData v4 Using ASP.NET Web API 2.2</strong></a><br>
  This article says I need to add a "CreateRef" in the Orders controller.<br>
  I did create a "CreateRef" in the Orders controller and sure enough it gets called BUT the article assumes the OrderDetail exists in the database.<br>
  It is not posting the json OrderDetail object.</p>
  
  <p><strong>Round 3</strong>  </p>
  
  <pre><code>// Add the new item with a link to the related Order
  context.AttachTo("OrderDetails", newOrderDetail);
  // Attach a link between Order and the new OrderDetail
  context.AttachLink(order, "OrderDetails", newOrderDetail);
  // Add the new order detail to the collection
  order.Order_Details.Add(newOrderDetail);
  newOrderDetail.Order = order;
  </code></pre>
  
  <p>Well this seems much better.<br>
  No error but it did not fully work.<br>
  It sent a PUT to the /odata/OrderDetails(0) and it did send the json OrderDetail object BUT this should have been a <strong>POST not a PUT</strong>.</p>
  
  <p>I feel I am so close yet I can't seem to figure out how to get it to work properly.</p>
  
  <p>Any ideas?</p>