How to address entity that uses composite identity key in OData Url?

流过昼夜 提交于 2020-06-27 07:08:21

问题


I have an entity OrderItem that has OrderId and ProductId integer fields and these two fields form the identity key/primary key for this table.
I would like to use OData/Web API to expose such entities through a service and to be able to select OrderItem instances by they composite ID.

What should be the format of the URL?

Are there any best practices for handling such scenarios?


回答1:


Composite keys in the URL use syntax like this:

~/OrderItems(OrderId=1234,ProductId=1234)

The "grammar" is defined in the OData ABNF Construction Rules (see the definition for "compoundKey")

An example usage can be found in OASIS' OData Version 4.0. Part 2: URL Conventions Plus Errata 03

Note that the "composite key" (aka "complex key predicate") has been around since OData 1.0.




回答2:


First, you have to make sure that you explicitly mention that it has a composite key, in configuration file

builder.EntityType<OrderItem>().HasKey(t => new { t.OrderId, t.ProductId});

Then, the action should have the following header

public SingleResult<OrderItem> Get([FromODataUri] string keyOrderId, [FromODataUri] string keyProductId)

Please note the prefix(key) used for both parameters!

This is OData V4. Please also refer to https://odata.github.io/WebApi/13-06-KeyValueBinding/



来源:https://stackoverflow.com/questions/12509111/how-to-address-entity-that-uses-composite-identity-key-in-odata-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!