Question: How to get ODATA to serialize NotMapped property


How to get ODATA to serialize NotMapped property

Answers 1
Added at 2017-01-05 19:01

I have a WebAPI backend that provides inventory information, etc. to various clients, using ODATA v3 (I cannot use v4 due to a restriction in a component that we use). The inventory database is quite large (100K+ records) and ODATA is great for server-side filtering, pagination, etc. and keeping the data transfers lean.

The inventory records have some properties that are not mapped, but rather calculated and populated on the fly as the queries are made. For example:

public decimal RebateAmount { get; set; }

The problem is that ODATA ignores any NotMapped properties, so they never get sent back to the clients.

I know this has been asked before, but that was a while ago now so I was hoping that support for this has been added by now or that somebody has a simple workaround (short of letting EF create these fields in the DB).

I have tried this ugly workaround, but it did not work (RebateAmount still does not get included by ODATA):

private decimal _rebateAmount;
public decimal RebateAmount { get {return _rebateAmount; } }

public void SetRebateAmount(decimal amount)
   _rebateAmount = amount;

Is there a (preferably simple) way to include non-DB properties in ODATA result sets?

Edit 1/7/2017 For this to be useful in my scenario, ODATA needs to also include the calculated fields in its metadata, otherwise the auto-generated (client-side) proxy classes will not include them.

nr: #1 dodano: 2017-01-07 08:01

One approach could be by creating OData EDMModel with a DTO object with the additional computed property.

Please check this answer which has the implemetation details to do this: Mapping OData query against a DTO to another entity?

The DTO can be mapped by a View at Database server or can be calculated in controller by using OdataQueryOptions.

Source Show
◀ Wstecz