OData:找不到在继承类型上命名的属性

本文关键字:属性 类型 找不到 继承 OData | 更新日期: 2023-09-27 18:36:47

对于我的例子,我有两个类

public class Location
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Address1 { get; set; }
    public string Address2 { get; set; }
    public string Address3 { get; set; }
    public string Town { get; set; }
    public string County { get; set; }
    public string CountryCode { get; set; }
}
public class Customer : Location
{
    public string BankAccountNumber { get; set; }
    public string BankSortCode { get; set; }
}

在我的查询中,我返回了所有位置和客户。

http://localhost:80/odata/Location?select=Id,Name,Town

但是,如果我尝试在客户中选择任何内容(编辑:所以我想要所有位置,但如果位置也是客户,则想要银行帐号),则会出现错误。

http://localhost:80/odata/Location?select=Id,Name,Town,BankAccountNumber
"The query specified in the URI is not valid. Could not find a property named 'BankAccountNumber' on type 'MyNamespace.Location'."

有没有办法选择继承类型中的字段,而不全选?谢谢。

OData:找不到在继承类型上命名的属性

根据 OData.org,有 2 个选项可用于查询派生类型:

  1. ~/Location!Customer/
  2. ~/Location/OfType('Customer')

因此,您的查询应如下所示:

http://localhost:80/odata/Location!Customer?select=Id,Name,Town,BankAccountNumber

http://localhost:80/odata/Location/OfType('Customer')?select=Id,Name,Town,BankAccountNumber

/编辑:千里指出,上述博客条目指的是OData V2。在 Odata4 中,使用以下语法访问继承的类型:

http://host/service/BaseType/Model.SubType

参考:http://docs.oasis-open.org/odata/odata/v4.0/os/part2-url-conventions/odata-v4.0-os-part2-url-conventions.html#_Toc372793786

不仅应添加类型名称,还应添加类型的命名空间。例如: http://services.odata.org/TripPinWebApiService/People('russellwhyte')/Trips(1001)/PlanItems/ODataSamples.WebApiService.Models.Flight?$select=StartsAt

Flight 类型继承自 PlanItem 类型。ODataSamples.WebApiService.Models是命名空间。

派生类型的更多详细信息,如果您发现规范太长而无法阅读,您可以参考带有一些实时示例的 http://www.odata.org/getting-started/advanced-tutorial/......