如何正确处理OData复杂类型关系

时间:2016-03-30 20:53:21

标签: asp.net-web-api odata asp.net-web-api2

尝试使用Entity Framework 6.1围绕典型的默认Northwind数据库构建WebAPI 2 / OData v4服务 我的WebApiConfig对复杂类型关系":

感到不满
    An exception of type 'System.InvalidOperationException' 
occurred in System.Web.OData.dll 
but was not handled in user code

    Additional information: The complex type 'ODataProductService.Models.Order_Detail' 
refers to the entity type 'ODataProductService.Models.Product' 
through the property 'Product'.

显然,在任何给定的数据库中,很可能会发生这些关系。 处理这个问题的正确方法是什么?

2 个答案:

答案 0 :(得分:1)

以下是我如何解决这个问题: 1)将以下语句添加到我的WebApiConfig.cs:

我已经制作了一个GitHub仓库,里面有一个可行的解决方案:

builder.EntitySet<Customer>("Customers");
            builder.EntitySet<Product>("Products");
            builder.EntitySet<Order>("Orders").EntityType.HasKey(o => o.OrderID);
            builder.EntitySet<Order_Detail>("Order Details").EntityType.HasKey(od => od.OrderID);
            builder.EntitySet<CustomerDemographic>("CustomerDemographics").EntityType.HasKey(cd => cd.CustomerTypeID);

我还制作了一个有效的解决方案:

https://github.com/eugene-goldberg/ODataProductService/

自述文件几乎描述了要注意的事项。

答案 1 :(得分:0)

您还可以使用OData V4的containment功能。使用包含,您可以避免为Order_Detail定义实体集。

相关问题