Eugene Goldberg
Eugene Goldberg

Reputation: 15564

how to properly handle OData complex type relationships

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?

Upvotes: 0

Views: 1061

Answers (2)

lencharest
lencharest

Reputation: 2935

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

Upvotes: 0

Eugene Goldberg
Eugene Goldberg

Reputation: 15564

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.

Upvotes: 1

Related Questions