Open the file App_Start/WebApiConfig.cs. Add the following using statements:
using ProductService.Models;
using System.Web.OData.Builder;
using System.Web.OData.Extensions;
Then add the following code to the Register method:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// New code:
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<Product>("Products");
config.MapODataServiceRoute(
routeName: "ODataRoute",
routePrefix: null,
model: builder.GetEdmModel());
}
}
This code does two things:
An EDM is an abstract model of the data. The EDM is used to create the service metadata document. The ODataConventionModelBuilder class creates an EDM by using default naming conventions. This approach requires the least code. If you want more control over the EDM, you can use the ODataModelBuilder class to create the EDM by adding properties, keys, and navigation properties explicitly.
A route tells Web API how to route HTTP requests to the endpoint. To create an OData v4 route, call the MapODataServiceRoute extension method.
If your application has multiple OData endpoints, create a separate route for each. Give each route a unique route name and prefix.