你能用相同的名称声明2个OData资源EntitySet吗?

时间:2016-03-04 02:23:06

标签: c# .net rest asp.net-web-api odata

如何声明两个具有相同名称但在不同区域下路由的OData EntitySet?这可能吗?

例如:

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());
}

处理不同的请求,例如:

  

获取http://localhost/MyApp/Costco/Foods

     

获取http://localhost/MyApp/Ikea/Foods

1 个答案:

答案 0 :(得分:2)

在同一EDM模型中,不能有2个具有相同名称的不同实体。您必须创建两个不同的EDM模型和路线,如下所示 -

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());