Entity Framework 4 allows us to create complex types. Typically, such types are used to get the result of a stored procedure. However, when we try to send such a complex type using a WCF RIA Domain Service, the following error is encountered.
The entity ' StoredProcedure_Result' in DomainService 'MyDomainService' does not have a key defined. Entities exposed by DomainService operations must have at least one public property marked with the KeyAttribute.
What to do? Adding attributes to EF designer generated classes is a bad idea; Also, we cannot use partial classes to add attributes to an already defined property. However, luckily, WCF RIA Services uses metadata classes in conjunction with the entity model classes and hence we can always create a meta data class for our complex type and decorate one of the fields with a [Key] attribute. E.g., If our stored procedure is returning StoredProcedure_Result, then we may just need to add the following in our DomainService.metadata.cs file:
[MetadataTypeAttribute(typeof(StoredProcedure_Result.StoredProcedure_Result_Metadata))]
public partial class StoredProcedure_Result
{
internal sealed class StoredProcedure_Result_Metadata
{
// Metadata classes are not meant to be instantiated, so mark constructor as private
private StoredProcedure_Result_Metadata()
{ }
[Key]
public string SomeUniqueColumn { get; set; }
}
}
November 25, 2010 at 10:32 AM
RIA Services: Sending complex types to the client « Mehroz’s Experiments…
Thank you for submitting this cool story – Trackback from DotNetShoutout…
April 27, 2011 at 8:32 PM
Excellent posting!, I’ve been fighting this issue for a few days, thanks for sharing it with the community.
June 13, 2011 at 7:39 AM
[...] LinkedIn [...]
July 30, 2011 at 2:33 AM
Thanks for sharing
August 3, 2011 at 8:37 AM
I cant find this file DomainService.metadata.cs could you please tell me where would this file be
thank you so much
November 1, 2011 at 9:26 PM
Hello and thank you for the post!
I followed your instructions and now wish to call my Sproc (complex type) but, I wish to pass in one or two parameters. Do you have an example / code snippet that you could share that shows how to setup a method in my domain service class to call a sproc while passing in a parameter?
Many thanks in advance!
~ Dave