inherited types in odata xml feed error

Topics: ASP.NET Web API
Sep 25, 2012 at 8:37 PM

when creating a controller for the base class, lets say it is a Vehicle (which is an abstract class) the odata xml returned will have a category entry looking like this (the actual object populated is a public class Car : Vehicle):

<category term="Namespace.Vehicle" scheme="" />

which points to an abstract class thus preventing the client from creating a real object instance (Car), so it should be:

<category term="Namespace.Car"scheme="" />

I tried modifying ODataEntityTypeSerializer.WriteEntry and changing from:


            ODataEntry entry = new ODataEntry
                TypeName = _edmEntityTypeReference.FullName(),
                Properties = propertyBag,



           ODataEntry entry = new ODataEntry
                TypeName = graph.GetType().FullName,
                Properties = propertyBag,


which will point to the correct actual instance of the object. This works fine if I have only one type of instance (aka Car). However if my repository returns both Car and Motorcycle (inherited from Vehicle) i get the following exception from ODataWriter saying:

"An entry of type 'Car' was found in a feed that otherwise has entries of type 'Motorcycle'. In OData, all entries in a feed must have a common base type."

Is that a bug in the ODataWriter, since clearly one feed can have different types which are base of one class since even error says that.

Anyone else had similar issue?

Sep 25, 2012 at 9:08 PM
Edited Sep 25, 2012 at 9:09 PM

If your action has a signature 


public Vehicle GetVehicle()


and returns a derived type instance Car, the entry type should be Namespace.Car.

Please refer to the test named 'Action_Can_Take_Entity_In_Inheritance' at link.

And for a feed of base type (vehicles) containing derived types (cars and motorcycles) please refer to the test case 'Action_Can_Return_CollectionOfEntities' at link.

Is your scenario different from one of these examples? If so can you paste what your action looks like ?

Sep 25, 2012 at 9:35 PM

Thanks for quick reply, I saw those tests checked in last night, right in time for my project.

I see that your tests are exactly what I would expect. Can it be due to the fact that you are using manual model builder and i use the convention one (ODataConventionModelBuilder)?

                .Property(c => c.SeatingCapacity);


Sep 25, 2012 at 9:40 PM

Hi Woland,

Thanks for trying out the bits. The ODataConventionModelBuilder does not support inheritance yet. We have done support in the manual model builder and formatter only yet. I am right now working on adding inheritance support to convention model builder and query($filter). I should be done very soon with that.

Sep 25, 2012 at 10:01 PM

Thanks, I'll be waiting for your check in then.

Oct 1, 2012 at 8:46 PM

Any progress on enabling Convention Builder?

Oct 1, 2012 at 8:50 PM

It is in the internal code review pipeline. I should be able to push it to the origin by EOD tomorrow.