简体   繁体   English

返回实体集合的 OData 函数因 PowerQuery 失败

[英]OData function returning a collection of entities fails with PowerQuery

I got this error while using PowerQuery on a OData service I'm currently developing:在我目前正在开发的 OData 服务上使用 PowerQuery 时出现此错误:

When writing a JSON response, a user model must be specified and the entity set and entity type must be passed to the ODataMessageWriter.CreateODataEntryWriter method or the ODataFeedAndEntrySerializationInfo must be set on the ODataEntry or ODataFeed that is being written.编写 JSON 响应时,必须指定用户模型,并且必须将实体集和实体类型传递给 ODataMessageWriter.CreateODataEntryWriter 方法,或者必须在正在写入的 ODataEntry 或 ODataFeed 上设置 ODataFeedAndEntrySerializationInfo。

This occurs when invoking from PowerQuery a bound function that returns a collection of entities.当从 PowerQuery 调用返回实体集合的绑定函数时,会发生这种情况。 When invoked from a web browser, the response is (JSON format):从 Web 浏览器调用时,响应为(JSON 格式):

{
    "@odata.context": "http://localhost:8080/ODataPrototype/ODataPrototype.svc/$metadata#Collection(Demo.ODataPrototype.Count)",
    "value": [
        {
            "RowCount": 1
        },
        {
            "RowCount": 2
        },
        {
            "RowCount": 3
        },
        {
            "RowCount": 4
        }
    ]
}

I use the Olingo V4 library.我使用 Olingo V4 库。 A stripped down version of my metadata would be:我的元数据的精简版本将是:

<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
  <edmx:DataServices>
    <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="Demo.ODataPrototype">
      <EntityType Name="Instance">
        <Key>
          <PropertyRef Name="Id" />
        </Key>
        <Property Name="Id" Type="Edm.Int32" />
        <Property Name="Name" Type="Edm.String" />
        <Property Name="Description" Type="Edm.String" />
        <Property Name="Tag" Type="Edm.String" />
        <Property Name="Xid" Type="Edm.Int64" />
        <Property Name="Properties" Type="Collection(Demo.ODataPrototype.Property)" />
      </EntityType>
      <EntityType Name="Count">
        <Property Name="RowCount" Type="Edm.Int32" />
      </EntityType>
      <ComplexType Name="Property">
        <Property Name="Name" Type="Edm.String" />
        <Property Name="Value" Type="Edm.String" />
      </ComplexType>
      <Function Name="GetData" EntitySetPath="Instance/Demo.ODataPrototype.Count" IsBound="true">
        <Parameter Name="Instance" Type="Demo.ODataPrototype.Instance" />
        <Parameter Name="From" Type="Edm.DateTimeOffset" />
        <Parameter Name="To" Type="Edm.DateTimeOffset" />
        <ReturnType Type="Collection(Demo.ODataPrototype.Count)" />
      </Function>
      <EntityContainer Name="Container">
        <EntitySet Name="Instances" EntityType="Demo.ODataPrototype.Instance"></EntitySet>
        <EntitySet Name="Count" EntityType="Demo.ODataPrototype.Count" />
      </EntityContainer>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>

Would anyone know what I'm missing?有谁知道我错过了什么?

I found the solution.我找到了解决方案。 The context in my JSON response defines the result as a collection of type IVserver.ODataPrototype.Count:我的 JSON 响应中的上下文将结果定义为 IVserver.ODataPrototype.Count 类型的集合:

"@odata.context": "http://localhost:8080/ODataPrototype/ODataPrototype.svc/$metadata#Collection(IVserver.ODataPrototype.Count)"

But PowerQuery needs an entityset:但是 PowerQuery 需要一个实体集:

"@odata.context": "http://localhost:8080/ODataPrototype/ODataPrototype.svc/$metadata#Count"

The EntitySet "Count" must be declared in the EntityContainer. EntitySet "Count" 必须在 EntityContainer 中声明。

To get the EntitySet in the @odata.context, an entityset must be set when building the ContextURL.要获取@odata.context 中的EntitySet,必须在构建ContextURL 时设置一个entityset。 Example:例子:

final ContextURL contextURL = ContextURL.with().entitySet(responseEdmEntitySet).selectList(selectList).serviceRoot(baseURI).build();

And, the methods asCollection() and type() must not be called.并且,不得调用方法asCollection()type()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM