OData / OData/AspNetCoreOData

OData, Net5: How to implement $count when controllers return entities of multiple types

Open
#351 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
505
Forks
186
PR merge metrics
No merged PRs in 30d

Description

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().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)?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with Startup.cs, ConfigureServices, and GetEdmModel, then trace the OrganizationController endpoints returning OrganizationFull and OrganizationList. Reproduce the duplicate EntitySet exception and the 404 for the list endpoint; done means establishing a supported configuration that preserves both endpoints and returns $count metadata.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.