Introduction to Derived Properties
What is a derived property?
A derived property is a property that does not exist on the EntityType associated with the EntitySet; rather it exists on a type that derives from the base type of the entity set. This feature has been among our customers top asks since the lack of support makes exposing models with rich, well-defined inheritance hierarchies impossible to do. To enable such scenarios WCF Data Services now supports both exposing and consuming models which have properties (primitive, complex & navigation) defined on subtypes of the base type associated with the set
How can I specify derived properties in CSDL?
As you know, WCF Data Services exposes a metadata document ($metadata endpoint) which describes the data model exposed by the service. Below is an example of a metadata document that defines derived properties. No new extensions have been added to support this functionality. Protocol changes need to support derived properties can be found here
How are derived properties used on the client?
A representation of the above example in a .NET type would be:
public class Person
{
public int ID { get; set; }
public string FirstName{ get; set; }
public string LastName{ get; set; }
}
public class Employee:Person
{
public string Building{get;set;}
public IList<Employee> Resports { get; set; }
public Employee Manager { get; set; }
}
Two new Linq operators are now supported on the client:
- OfType<T>: used when the type specification needs to happen in the path segment.
- As: used when the type appears in a query option (e.g. $filter, $select, $orderby)
Below are some examples of how to query derived properties on the client:
PeopleData ctx = newPeopleData(serviceUri);
// returns only Employee instances
foreach (Employee e in ctx.People.OfType<Employee>() )
{
. . .
}
// returns a narrow projection that mixes content from Person and Employee
var q = from c in ctx.People
select new EmployeeBuilding { Name = c.FirstName, Building = (c as Employee).Building };
foreach (EmployeeBuilding s in q)
{
. . .
}
// returns only entities that are Employees working in building 18
var q2 = from p in ctx.People
where (p.FirstName==”Bill” && ((p as Employee).Building == "18"))
select p;
foreach (Employee e in q2)
{
. . .
}
// returns entities of type Person, expanding derived Reports navigation property on the employee instances
foreach (Person p in ctx.People.Expand(p => (p as Employee).Reports))
{
. . .
}
Your feedback is appreciated.
Ahmed Moustafa
Program Manager
WCF Data Services
Comments
Anonymous
March 21, 2011
will it support the use of only keyword to return only instances of that type. Ef supports that since version 1. msdn.microsoft.com/.../bb399295.aspxAnonymous
March 27, 2011
Hello, I'm experiencing an error when using the OfType<> method. I get a DataServiceQueryException with the message "An error occurred while processing this request." The inner exception mentions not being able to find the specified resource. It seems to occur any time I use the OfType<> method in a client querying a WCF Data Service. Any ideas on what I'm doing wrong? Thanks.Anonymous
June 20, 2011
Michael, Did you ever solve this as I have teh same issue with CTP2 March 2011?Anonymous
November 01, 2011
Michael and Ian, Did any of you find a solution for this problem? I'm using October 2011 CTP and it still has the "OfType" issue.Anonymous
March 15, 2012
How can I use this with DataServiceQuery.AddQueryOption from silverlight?