Sooner or later you will encounter an issue with generated entity proxy classes if you use POCO objects in WCF RIA Services. The issue is you won't get any generated code for associations in your domain entity classes. Here is an example:

// "Master" domain entity class.

public class Parameter {
    [Key]
    public long Id { get; set; }

    public string Name { get; set; }

    public List<Option> Options { get; set; }
}

// "Details" domain entity class.

public class Option {
    [Key]
    public long Id { get; set; }

    public string Name { get; set; }
}

The generated Parameter entity proxy class won't have any reference to the Options property. To make that property generated you need to apply AssociationAttribute and IncludeAttribute on the Parameter.Options property, and slightly modify the Options class as shown below:

// "Master" domain entity class.

public class Parameter {
    [Key]
    public long Id { get; set; }

    public string Name { get; set; }

    [Include]
    [Association("Parameter_Options", "Id", "ParameterId")]
    public List<Option> Options { get; set; }
}

// "Details" domain entity class.

public class Option {
    [Key]
    public long Id { get; set; }

    public long ParameterId { get; set; }

    public string Name { get; set; }
}

Now we have the association between our domain entity classes (Parameter.Id - Option.ParameterId) included in the generated entity proxy class on the Silverlight side.

Continue of the story is here.

Updates

  1. For clarity the ReadOnly attributes were removed from identifier properties. (7.12.2009)
  2. Added link to continue of the story. (13.12.2009)

Comments

topsy.com wrote 12/6/2009 at 11:43 PM Pingback from topsy.com

Twitter Trackbacks for
        
        Working with associated POCO objects in WCF RIA Services
        [rofas.net]
        on Topsy.com
Nikhil Kothari wrote 12/7/2009 at 9:39 AM [ReadOnly(true)] gets in the way of serialization.

RIA Services actually uses [Editable(false)].

You might want to update the blog post to reflect that.
Yauhen wrote 12/7/2009 at 8:01 PM Nikhil, thank you for pointing me out. I replaced the ReadOnlyAttribute(true) with EditableAttribute(false) in my code, but for clarity I removed them from the post. It is irrelevant for the topic.
Ahmed Saeed wrote 2/10/2010 at 6:56 AM Thanks . Its very helpful
VJ wrote 3/19/2010 at 1:40 AM How does it support many to many relationships between POCO objects without having to have an intermediate object?

I have asked this in more detail here.
vjavle.blogspot.com/.../...any-to-many-in-ria.html

e.g. if you have Employees and Managers as POCO objects where an Employee can be reporting to more than one Manager or a Manager can have more than one employee reports, in nHibernate it allows them to be traversed either way without explicitly exposing middle table.

In RIA attributes, is there any way to do it using Association or any other attribute?

Yauhen wrote 3/19/2010 at 10:27 PM Vasant, it looks like such type of relationship (many-to-many) could be modeled only by using three classes.
Daniel wrote 9/20/2012 at 7:35 PM For m2m relations you should take a look at m2m4ria.codeplex.com
Yauhen wrote 9/20/2012 at 10:52 PM it is clever name Smile
thanks for the link