Search code examples
c#odataasp.net-web-apiasp.net-web-api-odata

Name of 'Container' as an entity is not allowed in Web API OData


My list of entities includes one called 'Container'. (My company deals with containers a lot and it is a fairly "Set In Stone" term.)

So when I modeled my data for them, I naturally called them Containers.

That means I have a class called Container.

I just got done wiring up all my controllers for Web API OData and when I try to compile a client using it, I get an error. (I am using the WCF Data Service Client nuget package.)

The error is because there is my entity's Container partial class and another partial class that holds all the root level collections, also called Container. (NOTE: I don't get this error for my service that is running the same entities via WCF Data Services version of OData.)

So, my question is: Is there any way to have an entity called Container and expose it over Web Api OData? (and not get the client compile time errors).

NOTE: If you try to repro this, the error occurs if the entity called 'Container' has an association to a something that is also a root item (so it is included in the DataServiceContext class called 'Cntainer'. For example if the Containers has an List of possible ContainerTypes and there is a root list of ContainerTypes then the parital classes try to merge and there is a conflict.


Solution

  • Turns out the ODataModelBuilder (that you use to define all your entity sets) has a ContainerName property.

    Setting that gives you a different name than the default of "Container"

    ODataModelBuilder builder = new ODataConventionModelBuilder();
    builder.ContainerName = "YourCustomContainerNameHere";