OData有多个键

问题描述 投票:3回答:1

我正在使用Microsoft.OData 6.11.0软件包,我希望能够允许API的用户使用三个属性之一(数据库主键,用户名或外部ID号)作为密钥。代表人的数据类型。属性的格式不同,可以轻松区分。我按如下方式设置了OData模型:

ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Person>("Person");
config.MapODataServiceRoute(
    routeName: "ODataRoute",
    routePrefix: "OData",
    model: builder.GetEdmModel());

在控制器中,我有:

[EnableQuery]
public SingleResult<Person> Get([FromODataUri] String key) {/*...*/}

[EnableQuery(PageSize = 100)]
public IQueryable<Widget> WidgetsFromPerson([FromODataUri] String key) {/*...*/}

它猜测提供了哪个标识符并返回适当的数据。这些工作:

GET http://localhost/app/OData/Person(1234)
GET http://localhost/app/OData/Person(999999999)
GET http://localhost/app/OData/Person(1234)/Widgets
GET http://localhost/app/OData/Person(999999999)/Widgets

这些给我一个404。

GET http://localhost/app/OData/Person('username')
GET http://localhost/app/OData/Person('username')/Widgets

如果我不能这样做,是否有一种替代语法可以用来通过用户名获取该人以及该用户的小部件?

通过API返回的元数据包括:

<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
  <edmx:DataServices>
    <Schema Namespace="MyModel" xmlns="http://docs.oasis-open.org/odata/ns/edm">
      <EntityType Name="AbstractPerson" Abstract="true">
        <Key>
          <PropertyRef Name="PersonId" />
        </Key>
        <Property Name="PersonId" Type="Edm.Int32" Nullable="false" />
      </EntityType>
      <EntityType Name="Person" BaseType="MyModel.Person">
        <Property Name="UserName" Type="Edm.String" />
        <NavigationProperty Name="Widgets" Type="Collection(MyModel.Widget)" />
      </EntityType>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>

谢谢!

c# odata
1个回答
3
投票

我确实认为你正在寻找的是一个类似于备用键的功能。

OData团队正致力于替代关键支持。您可以从here找到详细信息和样本

此外,你可以找到正在进行中的implementation

© www.soinside.com 2019 - 2024. All rights reserved.