In my previous post I mentioned how to specify an association between POCO objects in RIA Services. In that post I did not mention that you will get an error as soon as you try to do some operations on associated objects. The error might look like this: "EntitySet of type 'Option' does not support the 'Add' operation".

There are two ways to solve the issue. First of all, it is necessary to decide what type of association you have. In case of composition you just need to use a new CompositionAttribute (read more in Mathew Charles' post):

// "Master" domain entity class.

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

    public string Name { get; set; }

    [Include]
    [Composition]
    [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; }
}

If you do not have the compositional association, you have to specify operations in the corresponding domain service.

[EnableClientAccess]
public class ParametersDomainService : DomainService
{
    [Query]
    public IEnumerable<Parameter> GetParameters() { ... }
    [Insert]
    public void AddParameter(Parameter parameterWithOptions) { ... }
    [Update]
    public void UpdateParameter(Parameter parameterWithOptions) { ... }
    [Delete]
    public void DeleteParameter(Parameter parameterWithOptions) { ... }

    [Insert]
    public void AddOption(Option option) { ... }
    [Update]
    public void UpdateOption(Option option) { ... }
    [Delete]
    public void DeleteOption(Option option) { ... }
}

In the AddParameter, UpdateParameter and DeleteParameter methods you will receive the parameter object with associated options. It is up to you whether to perform operations on option objects together with operations on parameters or to do it in the corresponding methods. Note that AddOption, UpdateOption and DeleteOption should exist, but they could remain empty.

Comments

Cleyton wrote 2/9/2011 at 12:34 PM Hi,

thanks for this post about composition in Ria Services.

I was wondering if you have any source code that goes with the article so that I can download it?

Cheers

C
Yauhen wrote 2/9/2011 at 1:46 PM Hi Cleyton,

No, I don't have any source code prepared for this article. The code you can see I adopted from my pet project.

You can find some code in Mathew's article I referenced above: blogs.msdn.com/.../...support-in-ria-services.aspx

Hope it will help.
rob wrote 11/5/2011 at 10:12 PM Excellent article. Exactly the information I was looking for.