0
votes

Trying to build a WebAPI 2 / OData v4 service around a typical default Northwind database, using Entity Framework 6.1 My WebApiConfig is unhappy about "complex type relationships":

    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'.

Obviously, in any given database, these relationships are very likely to occur. What is the proper way of hanlding this?

2

2 Answers

1
votes

Here is how I gat this resolved: 1) added the following statements to my WebApiConfig.cs:

I have made a GitHub repo with a working solution in it:

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

I have also made a repo with a working solution:

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

The Readme file pretty much describes what to pay attention to.

0
votes

You could also use the containment feature of OData V4. Using containment, you can avoid defining an entity set for Order_Detail.