1
votes

I have been facing an issue with Web API 2.2 with Web API OData controller. I am doing a PoC in which I need to display the JSON output from my service with different column names than that of corresponding Model props (DB table columns).

(For ex: 'CompanyName' from Customer table should appear as 'cName' in JSON output of my service.)

I am using DB first approach with Northwind database, created a Model with ADO.NET EF and created a controller using OData EF. (all default no code changes so far)

Now, I have tried to get different names using

1) Data Contract and Data Member -> specifying directly on Model class (yes, auto generated one)

2) JsonProperty -> specifying directly on Model class (yes, auto generated one)

3) DTOs [it works but I don't want use DTOs]

Unfortunately, first 2 approaches are not working for me (not sure what I'm missing here) and DTOs I'm trying to avoid. I'm stuck on this all my day today, appreciate if you can point me to a right approach.

Note: Instead of OData controller if I use regular Web API controller, all works.

1

1 Answers

1
votes

I realize this is old, and I'm not sure which version of OData you are using but the simple answer is, you have to specify all of this information in the model builder.

Here's an example.

        var builder = new ODataConventionModelBuilder();
        var entity = builder.EntitySet<Model>("models").EntityType;
        entity.Name = "model";
        entity.Property(p => p.Id).Name = "id";
        entity.Property(p => p.Name).Name = "name";
        entity.Property(p => p.Description).Name = "description";

Good luck!