问题描述
这个问题也在
StackOverflow 。
在OData:Operations文档中,
第2.4节第4段向下,它在使用POST创建实体时读取,也可以在同一请求中创建链接。但是,我在努力完成这项工作时遇到了麻烦。类似的问题已经被问到在创建时多对多链接
,看起来如果没有批量请求,这种情况是不可能的。以下是我尝试使用此方法创建的方案 示例
OData读写服务。
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.
创建名为"测试产品"的新产品并使用JSON在单个POST中将其链接到Category(0)。
Create a new Product named "Test Product" and link it to Category(0) in a single POST using JSON.
我试过...
POST /OData/OData.svc/Products HTTP / 1.1
接受:application / json
内容类型:application / json
{" ID":99," Name": "试验产品与QUOT;,"描述" ;:"简单测试与QUOT;," RELEASEDATE" ;:" \ /日期(1210204800000)\ /"," DiscontinuedDate" ;:空,"评价" ;: 3 ,"价格":"99.99","类别":""http://services.odata.org/OData/OData.svc/Categories(0)"
}
和...
> POST /OData/OData.svc/Products HTTP / 1.1
接受:application / json
内容类型:application / json
{" ID":99," Name":" Test Product" ,"描述" ;:"简单测试与QUOT;," RELEASEDATE" ;:" \ /日期(1210204800000)\ /"," DiscontinuedDate" ;:空,"评价" ;: 3,"价格" :"99.99","类别":{" uri":" http://services.odata.org/OData/OData.svc/Categories(0)"}
}
两者都会导致失败。
任何帮助将不胜感激。提前致谢。
推荐答案
POST
接受:application / json
内容类型:application / json
Accept: application/json
Content-Type: application/json
{
" __ metadata":{" uri": ")"}}
}
{
"__metadata": {"uri": "http://services.odata.org/OData/OData.svc/Products(99)", "type": "ODataDemo.Product"},
"ID": 99, "Name": "Bread", "Description": "Whole grain bread", "DiscontinuedDate": null, "Rating": 4, "Price": "2.5",
"Category": { "__metadata": {"uri": "http://services.odata.org/(S(bo5ox0nxweq24pbgqkeogwt0))/OData/OData.svc/Categories(0)"}}
}
这篇关于如何使用OData在单个POST请求中正确创建和链接一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!