Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OData v4 Web API support for Composite Keys

I am building an OData Web Service using WebAPI and OData v4.

I was able to get the service to support composite keys through overriding the SelectAction method of the EntityRoutingConvention. However, in previous version of OData this was not needed. I personally believe it’s pretty messy and I feel like I am reinventing the wheel.

Is there any other way?

like image 743
Francesco Ferraioli Avatar asked Dec 18 '14 00:12

Francesco Ferraioli


1 Answers

Use attribute routing.

An example:

Model:

public class Product
{
    [Key]
    public int ID { get; set; }

    [Key]
    public string Name { get; set; }
}

Controller method for identifying the entity using composite keys:

[EnableQuery]
[ODataRoute("Products(ID={key1},Name={key2})")]
public IHttpActionResult Get([FromODataUri] int key1, [FromODataUri] string key2)
{
    // You business logic for retrieving the entity from your storage using the two keys and return
}

Request sample:

GET http://host/service/Products(ID=1,Name='Car')

No need to override routing convention.

like image 65
Yi Ding - MSFT Avatar answered Sep 28 '22 09:09

Yi Ding - MSFT