如何使用 OData 在单个 POST 请求中正确创建和链接一对一关系

发布于 2024-10-11 08:07:30 字数 2494 浏览 6 评论 0原文

在 OData:Operations 文档第 2.4 节第四段中,它写道,在使用 POST 创建实体时,也可以在同一请求中创建链接。但是,我在尝试完成这项工作时遇到了麻烦。在创建时就多对多链接提出了类似的问题,看起来如果没有批量请求,就不可能实现该特定场景。下面是我尝试使用此 示例 OData 读写服务

创建一个名为“测试产品”的新产品,并使用 JSON 将其链接到单个 POST 中的类别(0)。

我已经尝试过...

POST /OData/OData.svc/Products HTTP/1.1
接受:application/json
Content-Type: application/json

{ "ID": 99, "Name": "测试产品", "Description": "简单测试", "ReleaseDate": "\/日期(1210204800000)\/”,“停止日期”:null,“评级”:3,“价格”:“99.99”,“类别”:“http://services.odata.org/OData/OData.svc/Categories( 0)" }

和 ...

POST /OData/OData.svc/Products HTTP/1.1
接受:application/json
Content-Type: application/json

{ "ID": 99, "Name": "测试产品", "Description": "简单测试", "ReleaseDate": "\/日期(1210204800000)\/", "DiscontinuedDate": null, "评级": 3, "价格": "99.99", "类别": {"uri": "http://services.odata.org/OData/OData .svc/类别(0)"} }

两者都会导致失败。

使用原子格式的另一个示例...

<?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>

上面的结果创建了 201,但没有为链接到现有类别的新产品创建关联。

任何帮助将不胜感激。提前致谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长亭外,古道边 2024-10-18 08:07:30

Microsoft 的 Pablo Castro 回答了我的问题。他建议我在 Atom 有效负载中将大写“L”更改为小写。 IE。

<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Category" type="application/atom+xml;type=entry" title="Category" href="Categories(0)"/>

对于 JSON 有效负载,您需要包含“__metadata”才能使其正常工作。 IE。

{
 Prop1: ...,
 Prop2: ...,
 LinkProp1: { __metadata: { uri: "http://..." } }
}

Pablo Castro from Microsoft answered my question. He suggested in the Atom payload I change the capital "L", to lowercase. IE.

<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Category" type="application/atom+xml;type=entry" title="Category" href="Categories(0)"/>

For the JSON payload you need to include "__metadata" in order for it to work. IE.

{
 Prop1: ...,
 Prop2: ...,
 LinkProp1: { __metadata: { uri: "http://..." } }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文