在使用RIA服务时,如何使用Silverlight扩展实体? [英] How Can I Extend A Entity When Using RIA Services With Silverlight?

查看:140
本文介绍了在使用RIA服务时,如何使用Silverlight扩展实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Silverlight解决方案的服务器端,我有2个项目。


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

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



我添加了以下属性:



联系
{
public string FullName
{
get
{
return string.Format({0} {1},this.FirstName,this.LastName);
}
}
}
}

从服务器端测试,这个新属性存在并且正常工作。该属性不存在于Silverlight客户端。我尝试使用 Include 属性添加元数据类,但是由于字符串是一个原始类型,因此在编译时会收到以下错误:


实体类型Contact中的属性FullName不能用
标记为IncludeAttribute,因为String不是有效的实体类型。
实体类型不能是原始类型或简单类型,如字符串
或Guid。


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

解决方案

将[DataMember]添加到您的FullName属性。 这里是有关向ComplexTypes添加方法/属性的一些说明。他们也可以申请实体。也许使用好友类,我没有尝试过这个实体。

 命名空间Server.DAL.Model 
{
public partial class Contact
{
[DataMember]
public string FullName
{
get
{
return string.Format({0} {1},this.FirstName,this 。姓);
}
}
}
}


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?

解决方案

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);
            }
        }
    }
}

这篇关于在使用RIA服务时,如何使用Silverlight扩展实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆