Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 283
DaveB Avatar asked Jul 19 '11 18:07

DaveB


2 Answers

You should put the code you have shared into a file called Contact.shared.cs. The WCF RIA tooling takes this code exactly and creates a file in the Silverlight project with that code. The client-side code then has access to this member and a duplication of the code compiled in the server project.

Here is more information on shared code in the MSDN docs.

like image 34
Ed Chapel Avatar answered Oct 06 '22 06:10

Ed Chapel


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);
            }
        }
    }
}
like image 123
Derek Beattie Avatar answered Oct 06 '22 05:10

Derek Beattie