Search code examples
c#asp.netentity-frameworkwcf-data-services

Exposing additional properties on entities in WCF Data Services


I feel I may be missing something critical (or just simple) here, however I can't get this working.


Given I have created an ASP.NET (.NET 4.0) web application project, in which I host a WCF Data Service. I have generated an EF 5.0 model of POCO entities from an existing database, and as usual the entity classes are partial.

I figure,

Gee, I'll "extend" these partial classes to expose additional (non-persisted, calculated) properties* in the data service.

* For the sake of brevity, let's assume a Person entity, to which I want to add a FullName property which concatenates FirstName and LastName.

Anyway, I go about my business continuing the partial class (in the same namespace of course)

public partial class Person {
    public string FullName {
        get { return this.FirstName + " " + this.LastName; }
    }
}

However, when I query a Person entity in the service, no FullName. When I query the service's $metadata, no definition of FullName.

What (if anything) needs to be done to support this?


Notes

  • All entities are visible via config.SetEntitySetAccessRule("*", EntitySetRights.All);
  • I've tried decorating with various attributes with no success, including EdmScalarPropertyAttribute and DataMemberAttribute on the property.

Solution

  • Unless it changed in .NET 4.5 (I haven't looked) I don't think that can work. DataService<T> is extremely limited. It doesn't even support all EF features. Data services themselves are quite flexible, and you can define an entirely custom service which returns anything you want. But then you don't get the "automatic" mapping of your EF context.

    So you unfortunately have to choose between "easy but limited" and "difficult but flexible" with almost nothing in between.