Search code examples
wcfinheritanceodata

How do you Expose Inherited Entities using WCF and OData?


If I have an OData inheritance hierarchy exposed by WCF data services how do I expose the subclass entity as its own entity set? What URI should I use to access it?

Let's say I have an OData URL as follows:

http://myodataservice.svc

Let's say I have an entity foo that is abstract and an entity bar that is a subclass of foo.

What I'd like to do is go to a URL http://myodataservice.svc/bar to access the bar object but it seems that this is not possible. Can anyone point me at a simple example so I can see how this works?


Solution

  • Each entity instance can only belong to one entity set. So you can have multiple entity sets which share the same type (or base type), but their sets of entity instances must not overlap. If you need to have an entity set which contains all the entity instances of the base type, but still want to sometime access only entity instances of a certain derived type, you can use the type cast path segment.

    This requires OData V3, but then you can do something like this:

    http://myodataservice.svc/baseentities/Namespace.DerivedType

    This URL acts like an entity set of entities from the baseentities entity set, but filtered to only the DerivedType instances. It is also strongly typed (the type of that URL is a collection of DerivedType instances). So you can use it just like any other entity set URL, that is you can further navigate using / or you can append any of the query operators like $filter, $select and so on.