Search code examples
silverlightsilverlight-4.0silverlight-3.0wcf-ria-services

Custom property in RIA service not available on client


I have an entity called StatusUpdates. There is no self join in it. However i want to include a list of InternalStatusUpdates which is of type StatusUpdates (Again i would mention there is no self join in database). So i created a partial class in the same namespace and created a property named InternalStatusUpdates and included [DataMember] attribute on it. But it still doesn't appear on the client. This is my partial class to be specific :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using System.Runtime.Serialization;
using System.ServiceModel.DomainServices.Server;
using System.ComponentModel.DataAnnotations;

namespace FMT.Data.Web
{
    public partial class StatusUpdate : EntityObject
    {

         private List<StatusUpdate> _internalListStatusUpdates = new List<StatusUpdate>();
         [DataMember]
         public List<StatusUpdate> InternalListStatusUpdates
         {
             get { return _internalListStatusUpdates; }
             set {
                 _internalListStatusUpdates = value;
                 OnPropertyChanged("InternalListStatusUpdates");
             }
         }
    }
}

Interesting thing is if i change List<StatusUpdate> to List<string> it is available on the client! How do i solve this problem?

I thought because it is entity type i will have to specify [Include]. When i did that i get the following error :-

Invalid Include specification for member 'StatusUpdate.InternalListStatusUpdates'. Non-projection includes can only be specified on members with the AssociationAttribute applied.

Then i went ahead and specified association, so now my property looks like :-

private List<StatusUpdate> _internalListStatusUpdates = new List<StatusUpdate>();
     [DataMember]
    [Include]
    [Association("internalUpdates", "StatusUpdatesId", "StatusUpdatesId")]
     public List<StatusUpdate> InternalListStatusUpdates
     {
         get { return _internalListStatusUpdates; }
         set {
             _internalListStatusUpdates = value;
             OnPropertyChanged("InternalListStatusUpdates");
         }
     }

where StatusUpdatesId is my primary key in StatusUpdates Entity and internalUpdates is just a arbitary name chosen. Now the build is successfull and the property is available on the client but the data that i populate from server is not available on the client. How do i solve it?

Thanks in advance :)


Solution

  • The third parameter in AssociationAttribute is the name of the foreign-key property in the list elements. It is important to keep in mind that RIA Services puts entities in their corresponding EntitySet<T> and then applies filters on primary key and foreign key instead of using true references to the other objects.

    In the given case you will probably have to define a property "ParentUpdatesId" for your list elements and fill that with the primary key of your parent StatusUpdate.

    Example:

    StatusUpdate su = new StatusUpdate { StatusUpdatesId = 123 };
    StatusUpdate child1 = new StatusUpdate { StatusUpdatesId = 1001, ParentUpdatesId = 123 };
    StatusUpdate child2 = new StatusUpdate { StatusUpdatesId = 1002, ParentUpdatesId = 123 };
    su.InternalListStatusUpdates.Add(child1);
    su.InternalListStatusUpdates.Add(child2);
    

    Your Association would then look like this:

    [Include]
    [Association("internalUpdates", "StatusUpdatesId", "ParentUpdatesId")]
    public List<StatusUpdate> InternalListStatusUpdates { ... }