Search code examples
silverlightentity-framework-4wcf-ria-services

How Can I Extend A Entity When Using RIA Services With Silverlight?


On the server side of my Silverlight solution, I have 2 projects.

  1. Website that serves up the Silverlight page.
  2. A Entity Framework data access layer.

I have a entity with FirstName and LastName properties on it. I want to add a FullName property that will be available from the Silverlight client side.

I have added the property:

namespace Server.DAL.Model
{
    public partial class Contact
    {
        public string FullName
        {
            get
            {
                return string.Format("{0} {1}", this.FirstName, this.LastName);
            }
        }
    }
}

When tested from the server side, this new property is present and working correctly. The property is NOT present on the Silverlight client side. I tried adding a metadata class with the Include attribute but since string is a primitive type, I get the following error on compilation:

The property 'FullName' in entity type 'Contact' cannot be marked with the IncludeAttribute because 'String' is not a valid entity type. Entity types cannot be a primitive type or a simple type like string or Guid.

How can I make this property available to the Silverlight client?


Solution

  • Add [DataMember] to your FullName property. Here are some instructions on adding methods/properties to ComplexTypes. They might apply to entities as well. Maybe using a buddy class, I haven't tried this for entities.

    namespace Server.DAL.Model
    {
        public partial class Contact
        {
            [DataMember]
            public string FullName
            {
                get
                {
                    return string.Format("{0} {1}", this.FirstName, this.LastName);
                }
            }
        }
    }