OData / OData/AspNetCoreOData

Building an EdmModel with inheritance relationship between entities

Open
#874 4 comments 0 reactions 1 assignee View on GitHub

@gathogojr is already working on this.

Since Apr 4, 2023.

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

Description

Hi,

I can't seem to find the right way to build my Edm model with the following requirement :

  • a base class and some derived ones, adding new specific properties
public class EntityA
{
    public Guid Id { get; set; }

    public string PropertyA { get; set; }
}

public class EntityB: EntityA
{
    public string PropertyB { get; set; }
}
  • a controller for the whole inheritance chain, calling the default route would return a collection of EntityAs, the /B route a collection of EntityBs, and so on
[Route("[controller]")]
public class ControllerA : ODataController
{
    [HttpGet]
    [EnableQuery(PageSize = 12)]
    [ProducesResponseType(typeof(IQueryable<EntityA>), 200)]
    public Task<IActionResult> GetAllA()
    {
        IQueryable<EntityA> allA = someService.GetAllA();
        return allA;
    }

    [HttpGet("B")]
    [EnableQuery(PageSize = 12)]
    [ProducesResponseType(typeof(IQueryable<EntityB>), 200)]
    public Task<IActionResult> GetAllB()
    {
        IQueryable<EntityB> allB = someService.GetAllB();
        return allB;
    }
}

When I declare my Edm model using the ODataConventionModelBuilder, I cannot find a way to declare the GetAllB method as a Function on the EntitySet<EntityA> which returns a collection of EntityB.
Either I tell it it ReturnsCollection<EntityB>(), and then when the convention builder map the types hierarchy internally, it cannot register EntityB as an EntityType because it is already registered as a ComplexType, or I try to make a call to ReturnsCollectionFromEntitySet<>() but then I have to register a new EntitySet with a different name (which I think I understood will not match anymore with my odata/A/B route ?) or to bind it to the EntitySet<EntityA> and then OData sends me back the wrong type in the response's odata.context property (which I need to be exact to perform some client-side operations for displaying the data).

What did I miss ?

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.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.