In the OData:Operations documentation, section 2.4 fourth paragraph down, it reads when creating an entity with POST it is also possible to create a link within the same request. However, I'm having trouble trying to make this work. A similar question has been asked about many-to-many linking while creating and it looks like that particular scenario isn't possible without a batch request. Below is the scenario I'm trying to create using this sample OData read write service.
Create a new Product named "Test Product" and link it to Category(0) in a single POST using JSON.
I have tried...
POST /OData/OData.svc/Products HTTP/1.1
Accept: application/json
Content-Type: application/json
{ "ID": 99, "Name": "Test Product", "Description": "Simple Test", "ReleaseDate": "\/Date(1210204800000)\/", "DiscontinuedDate": null, "Rating": 3, "Price": "99.99", "Category":"http://services.odata.org/OData/OData.svc/Categories(0)" }
and ...
POST /OData/OData.svc/Products HTTP/1.1
Accept: application/json
Content-Type: application/json
{ "ID": 99, "Name": "Test Product", "Description": "Simple Test", "ReleaseDate": "\/Date(1210204800000)\/", "DiscontinuedDate": null, "Rating": 3, "Price": "99.99", "Category": {"uri": "http://services.odata.org/OData/OData.svc/Categories(0)"} }
Both which result in failures.
Another example using atom format...
<?xml version="1.0" encoding="utf-8"?>
<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text"/>
<updated>2010-02-27T21:36:47Z</updated>
<author>
<name/>
</author>
<Link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Category" type="application/atom+xml;type=Entry" title="Category" href="Categories(0)"/>
<category term="ODataDemo.Product" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
<content type="application/xml">
<m:properties>
<d:ID m:type="Edm.Int32">99</d:ID>
<d:Name m:type="Edm.String">New Product</d:Name>
<d:ReleaseDate m:type="Edm.DateTime">1992-01-01T00:00:00</d:ReleaseDate>
<d:DiscontinuedDate m:type="Edm.DateTime" m:null="true"/>
<d:Rating m:type="Edm.Int32">4</d:Rating>
<d:Price m:type="Edm.Decimal">2.5</d:Price>
</m:properties>
</content>
</entry>
The above results in a 201 created, yet no association is created for the new Product linking to the existing Category.
Any help would be appreciated. Thanks in advance.