I am adding OData to an existing large API in .Net5, where the action methods in each controller return different types (such as OrganizationFull, OrganizationList, etc.)
This is working, except that the responses do not contain OData meta data, such as $count. So when a consumer calls /api/OrganizationList?$count=true, it doesn't get the count.
I understand that to get this meta data, I have to add an EDM model, like so:
// Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddOData(opt => opt.Select().Filter().Count().SetMaxTop(10).AddRouteComponents("odata", GetEdmModel()))
IEdmModel GetEdmModel()
{
var odataBuilder = new ODataConventionModelBuilder();
odataBuilder.EntitySet<OrganizationFull>(nameof(Organization));
odataBuilder.EntitySet<OrganizationList>(nameof(Organization)); // throws
return odataBuilder.GetEdmModel();
}
Here I specified 2 EntitySets, one for each entity type returned from the OrganizationController.
However, this throws an exception "The entity set 'Organization' was already configured with a different EntityType ('OrganizationFull').". And if I remove the EntitySet for OrganizationList, when I hit the List end point, I get a 404.
How can I get my end points to return the $count meta data, while keeping my API (which returns multiple entity types from one controller)?
Alternatively, is there a way to get the count in the action method from the OData infrastructure? In that case I could return it in a response header.