RIA Services: Sending complex types to the client

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; }
    }
}
About these ads

6 Responses to “RIA Services: Sending complex types to the client”

  1. DotNetShoutout Says:

    RIA Services: Sending complex types to the client « Mehroz’s Experiments…

    Thank you for submitting this cool story – Trackback from DotNetShoutout…

  2. Luis Quintero Says:

    Excellent posting!, I’ve been fighting this issue for a few days, thanks for sharing it with the community.

  3. Omid Says:

    Thanks for sharing :)

  4. John Stern Says:

    I cant find this file DomainService.metadata.cs could you please tell me where would this file be

    thank you so much

  5. David Navarro Says:

    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


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 47 other followers

%d bloggers like this: