具有多个键的 OData

OData with multiple keys

我正在使用 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) {/*...*/}

猜测提供的标识符和 returns 适当的数据。这些工作:

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>

谢谢!

我认为您正在寻找的是类似于备用键的功能。

OData 团队正在致力于备用密钥支持。您可以在 here

中找到详细信息和示例

此外,您可以找到implementation进行中。