在使用RIA服务时,如何使用Silverlight扩展实体? [英] How Can I Extend A Entity When Using RIA Services With Silverlight?
问题描述
在Silverlight解决方案的服务器端,我有2个项目。
- 提供Silverlight页面的网站。 >
- 实体框架数据访问层。
我有一个具有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.
- Website that serves up the Silverlight page.
- 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屋!