How to create a computed property in Data Services (OData)?

I am creating an OData service with WCF data services using EDMX. How can I create a computed property for an entity type so that its value is computed in the service (C #) and not coming from the database?
The value of this property is based on the value of other properties that map to fields in the database.

+6
odata wcf-data-services edmx
source share
2 answers

The solution I found is to use Entity Framework Code First instead of EDMX. It allows you to create computed properties by simply creating standard properties in your code.
Here is an example:

public class Person { public String FirstName { get; set; } public String LastName { get; set; } public String FullName { get { return FirstName + " " + LastName; } } } 
+2
source share

If you directly display your EDMX file using the default Entity Framework provider for data services, complete the following steps:

 public class MyService: DataService<MyEntities> { 

Then, unfortunately, you cannot disclose any โ€œnewโ€ properties that are not in the EDM Entity Framework base model.

Having said that you have other options, you can write a reflection provider or a custom provider that will add an extra property and delegate most of the work of EF under the hood.

The problem is setting up the entire delegation is not easy today.

This series of messages explains the providers and shows how to create a custom service based on the provider, and this one shows how to create a service using the Reflection provider.

+2
source share

All Articles