Search code examples
c#.netrestasp.net-web-apiodata

Can you declare 2 OData resource EntitySets with the same name?


How do I declare two OData EntitySets with the same name but routed under different areas? Is this possible?

For example:

public static void Register(HttpConfiguration config)
{
   var builder = new ODataConventionModelBuilder();
   builder.EntitySet<Costco.Models.Food>("Foods");
   builder.EntitySet<Ikea.Models.Food>("Foods"); // this causes an exception
   config.Routes.MapODataServiceRoute("MyRoute", "{area}", builder.GetEdmModel());
}

to handle different requests like:

GET http://localhost/MyApp/Costco/Foods

GET http://localhost/MyApp/Ikea/Foods


Solution

  • You cannot have 2 differnet entities with same name in same EDM model. You have to create two different EDM models and routes like below -

    var costcoBuilder = new ODataConventionModelBuilder();
    costcoBuilder.EntitySet<Costco.Models.Food>("Foods");
    
    var ikeaBuilder = new ODataConventionModelBuilder();
    ikeaBuilder.EntitySet<Ikea.Models.Food>("Foods");
    
    config.Routes.MapODataServiceRoute("CostcoRoute", "Costco", costcoBuilder.GetEdmModel());
    config.Routes.MapODataServiceRoute("IkeaRoute", "Ikea", ikeaBuilder.GetEdmModel());