Search code examples
c#entity-frameworkjson.netodataef-core-2.2

How to group properties together when serializing to JSON with JsonConverter<T>


I will change my question all together leaving the initial question at the bottom.

I will try to explain what I'm trying to achieve without providing the underlying solution as I did before.

Time is not on my side unfortunately so please bear with me.

Here goes my question:

We use an old Oracle database which was modelled more than twenty years ago. It contain tables with hundred of columns and million of lines.

We need to expose some of those tables through an api, and as we have multiple use cases I thought it would be a good idea to use Odata so everyone can meet their requirements without have to create specific models for everyone.

I did not find a single solution using ODP.Net that would allow me to expose IQueryables without loading the entire tables (again, millions of lines). This is why I felt my only solution would be EF Core database first.

I was able to create a model based on the existing db model but what I’m trying to achieve is to split those huge table in multiple smaller models that would make more sense without losing odata capabilities.

My apologies if my initial question was misleading and if it did not provide the underlying problem.

Does anyone could help me do the same as described in this [question](https://stackoverflow.com/questions/36656880/how-to-group-properties-together-when-serializing-to-json) but with net8 and [JsonConverter](https://learn.microsoft.com/en-us/dotnet/api/system.text.json.serialization.jsonconverter-1?view=net-8.0) ?

The idea is to group properties using attributes on classes generated with ef core database first

Thanks !

EDIT: Let's say I have a table 'Person' with the following properties:

  • ID
  • Name
  • Address Line 1
  • Address Line 2
  • Address Line 3

And I'd like to regroup all the Address properties into a group named 'Address'

What would be the best solution to do so ?

Edit 2:

Currently I'm exposing an Odata controller which targets directly my dbcontext and my dbsets:

[MapToApiVersion("2.0")]
[HttpGet]
[ResponseCache(Duration = 30, VaryByQueryKeys = new[] { "*" })]
[ProducesResponseType(StatusCodes.Status200OK)]
[EnableQuery]
public IActionResult Get()
{
    return Ok(JsonConvert.SerializeObject(_context.Address), Formatting.Indented));
}

Solution

  • For those wondering I managed to do what I needed exposing Views on top of my tables which helped me redifine the functional schema.

    I then used Fluent Api to target the views instead of the tables.