Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OData with multiple keys

Tags:

c#

odata

I'm using the Microsoft.OData 6.11.0 package, and I'd like to be able to allow users of the API to use one of three properties (DB primary key, username, or external ID number) as the key for a data type representing people. The formats for the properties are different enough that they can be distinguished between easily. I set up the OData model as follows:

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

In the controller, I have:

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

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

which takes a guess at which identifier is provided and returns the appropriate data. These work:

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

These get me a 404.

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

If I can't do this, is there an alternate syntax I can use to get the person by username as well as the widgets for that person by username?

The metadata returned through the API includes this:

<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>

Thanks!

like image 758
Don 01001100 Avatar asked May 15 '26 20:05

Don 01001100


1 Answers

I do think what you're looking for is a feature similar to alternate key.

OData team is working on alternate key supporting. You can find the detail information and sample from here

Besides, you can find the implementation in progress.

like image 64
Sam Xu Avatar answered May 18 '26 09:05

Sam Xu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!