views:

31

answers:

2

I am creating an OData service with WCF Data Services using an EDMX. How can I create a computed property for an entity type, so that its value gets computed in the service (C#) and does not come from the database?
The value of this property is based on the value of other properties, which are mapped to fields in the database.

A: 

If you are exposing your EDMX file directly, using the default Entity Framework Provider for Data Services, something like this:

public class MyService: DataService<MyEntities> {

Then unfortunately you can't expose any 'new' properties that aren't in the underlying Entity Framework EDM model.

Having said that you have other options, you could write a reflection provider or custom provider that adds the extra property and delegates most of the work to EF under the hood.

The problem is setting up all the delegation is NOT easy today.

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

Alex James
Thanks Alex. I'll mark your reply as the accepted answer, although I found an easier solution: instead of using an EDMX, I use Entity Framework Code First; it allowed me to create computed properties just by creating properties in code. I will document this in a blog post.
Fabrice
A: 

The solution I found is to use Entity Framework Code First instead of an EDMX. It allows you to create computed properties just by creating standard properties in 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; }
  }
}
Fabrice