在Silverlight中使用RIA服务时如何扩展实体?

前端之家收集整理的这篇文章主要介绍了在Silverlight中使用RIA服务时如何扩展实体?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的Silverlight解决方案的服务器端,我有2个项目.

>提供Silverlight页面的网站.
>实体框架数据访问层.

我有一个具有FirstName和LastName属性的实体.我想添加一个可以从Silverlight客户端获得的FullName属性.

添加属性

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

从服务器端进行测试时,此新属性存在且工作正常.该属性不存在于Silverlight客户端.我尝试使用Include属性添加元数据类,但由于string是基本类型,因此在编译时出现以下错误

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.

如何将此属性提供给Silverlight客户端?

解决方法

将[DataMember]添加到FullName属性. Here是有关向ComplexTypes添加方法/属性的一些说明.它们也可能适用于实体.也许使用好友类,我没有尝试过这个实体.

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

猜你在找的Silverlight相关文章