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

12 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

  6. Anton Says:

    Pretty element of content. I just stumbled upon
    your blog and in accession capital to say that I acquire actually loved account your weblog
    posts. Anyway I’ll be subscribing in your augment or even I success you get right of entry to consistently rapidly.

  7. Desmond Says:

    Hello would you mind stating which blog platform you’re using? I’m going to start
    my own blog soon but I’m having a hard time choosing between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I’m looking
    for something completely unique. P.S My apologies for getting off-topic but
    I had to ask!

  8. mould removal in toronto Says:

    Do you have a Twitter profile that we could become followers of?

    And where can we read more related blogposts that you authored in the past?

  9. mold removal companies toronto Says:

    Will there be an RSS feed that we may sign up to on this site so if you have new content that
    we could be notified?

  10. http://www.lockwoodbroadcast.com Says:

    If you find it a bit maddening to follow the daily fluctuations of
    the scale even though you are eating properly, pick three days of the week on which you will
    always weigh yourself (for example, Monday, Wednesday, and Friday).
    Authorities inside the area will need to have an intensive understanding of laboratory
    and labeling techniques, infection security precautions, appropriate blood
    attract approaches to the elderly and for infants and even more.
    That way, the actual process will end up being much less frustrating and, for
    the majority of people, enjoyable.

  11. sitio web Says:

    If some one wishes expert view on the topic of running a blog afterward
    i propose him/her to go to see this webpage,
    Keep up the good job.


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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 54 other followers

%d bloggers like this: