【问题标题】:how to properly handle OData complex type relationships如何正确处理 OData 复杂类型关系
【发布时间】:2016-03-30 20:53:21
【问题描述】:

尝试使用 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'.

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

【问题讨论】:

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


    【解决方案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/

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2013-12-17
        • 2021-10-07
        • 1970-01-01
        • 2013-05-26
        • 1970-01-01
        • 1970-01-01
        • 2015-05-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多