Search code examples
c#asp.net-web-apiodataasp.net-web-api2

How to make dynamic edm model support odata queries


I have been trying to adapt the DynamicEdmModelCreation sample from https://github.com/OData/ODataSamples/tree/master/WebApi/v4/DynamicEdmModelCreation into a workable v3 version (I want to load odata directly into excel, which doesn't support odata v4 yet)

I added config.AddODataQueryFilter() to the setup and [EnableQuery] on the Controller's get method, but I now receive the exception:

A first chance exception of type 'System.Runtime.Serialization.SerializationException' occurred in System.Web.Http.OData.dll

Additional information: 'EnumerableQuery`1' cannot be serialized using the ODataMediaTypeFormatter.

Which is being thrown from https://github.com/ASP-NET-MVC/aspnetwebstack/blob/master/OData/src/System.Web.Http.OData/OData/Formatter/ODataMediaTypeFormatter.cs#L577

Is the fact that my Controller's Get method is returning an EdmEntityObjectCollection an issue? I would have thought that just adding the EnableQuery attribute would be enough for the framework to know how to apply that query operation to the EdmEntityObjectCollection that I'm returning (which will have lots more data than potentially needed)


Solution

  • What you have done is right, but OData WebApi doesn't support query options in un-typed scenario now, and there is an open issue about this feature.