Alternate key in WebApi
Applies To:# OData WebApi v7 for aspnet webapi supported OData AspNet WebApi V7# OData Webapi for Webapi supported OData AspNet WebApi V6
Alternate keys is supported in Web API OData V5.7. For detail information about alternate keys, please refer to here
The related sample codes can be found here
Enable Alternate key
Users can enable alternate key in the global configuration.
HttpConfiguration config = ...
config.EnableAlternateKeys(true);
config.MapODataServiceRoute(...)
Model builder
So far, an Edm model with alternate keys can be built by ODL APIs.
EdmEntityType customer = new EdmEntityType("NS", "Customer");
customer.AddKeys(customer.AddStructuralProperty("ID", EdmPrimitiveTypeKind.Int32));
customer.AddStructuralProperty("Name", EdmPrimitiveTypeKind.String);
var ssn = customer.AddStructuralProperty("SSN", EdmPrimitiveTypeKind.String);
model.AddAlternateKeyAnnotation(customer, new Dictionary<string, IEdmProperty>
{
{"SSN", ssn}
});
model.AddElement(customer);
So, SSN is an alternate key.
Routing for alternate key
In OData controller, Users can use the attribute routing to route the alternate key. The Uri template is similar to function parameter. For example:
[HttpGet]
[ODataRoute("Customers(SSN={ssn})")]
public IHttpActionResult GetCustomerBySSN([FromODataUri]string ssn)
{
...
}
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for