[英]OData function returning a collection of entities fails with PowerQuery
在我目前正在开发的 OData 服务上使用 PowerQuery 时出现此错误:
编写 JSON 响应时,必须指定用户模型,并且必须将实体集和实体类型传递给 ODataMessageWriter.CreateODataEntryWriter 方法,或者必须在正在写入的 ODataEntry 或 ODataFeed 上设置 ODataFeedAndEntrySerializationInfo。
当从 PowerQuery 调用返回实体集合的绑定函数时,会发生这种情况。 从 Web 浏览器调用时,响应为(JSON 格式):
{
"@odata.context": "http://localhost:8080/ODataPrototype/ODataPrototype.svc/$metadata#Collection(Demo.ODataPrototype.Count)",
"value": [
{
"RowCount": 1
},
{
"RowCount": 2
},
{
"RowCount": 3
},
{
"RowCount": 4
}
]
}
我使用 Olingo V4 库。 我的元数据的精简版本将是:
<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>
有谁知道我错过了什么?
我找到了解决方案。 我的 JSON 响应中的上下文将结果定义为 IVserver.ODataPrototype.Count 类型的集合:
"@odata.context": "http://localhost:8080/ODataPrototype/ODataPrototype.svc/$metadata#Collection(IVserver.ODataPrototype.Count)"
但是 PowerQuery 需要一个实体集:
"@odata.context": "http://localhost:8080/ODataPrototype/ODataPrototype.svc/$metadata#Count"
EntitySet "Count" 必须在 EntityContainer 中声明。
要获取@odata.context 中的EntitySet,必须在构建ContextURL 时设置一个entityset。 例子:
final ContextURL contextURL = ContextURL.with().entitySet(responseEdmEntitySet).selectList(selectList).serviceRoot(baseURI).build();
并且,不得调用方法asCollection()和type() 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.