If you use a reflection provider and your classes do not follow the convention for key properties, then you should use the DataServiceKey attribute. The reflection provider is the one you get if you simply provide the class definitions and the DataService context class. Therefore, if you do not implement IDataServiceMetadataProvider, you are most likely using a reflection provider. It is possible to use a reflection provider without attributes on your classes, but then WCF data services use heuristics to determine key properties. This happens as follows:
- if the class in question has a property called an identifier, this is an object with an identifier as the only key property.
- if the class is called, for example, by the Client, and it has a property called CustomerID, this is an object with the Property CustomerID as the only key property (the class name is obvious, like an example).
No other properties are recognized as key properties without the DataServiceKey attribute. This is also described, for example, in this blog: http://blogs.msdn.com/b/alexj/archive/2010/06/11/tip-56-writing-an-odata-service-using-the-reflection- provider.aspx
You can use 100% POCO classes with arbitrary key properties, but then you will have to implement a custom provider. This is much more work, since you must programmatically determine the shape of your classes. An example of a custom provider profile can be found here: http://blogs.msdn.com/b/alexj/archive/2010/01/07/data-service-providers-getting-started.aspx
Vitek Karas MSFT
source share