WCF 数据服务,从实体框架部分类序列化附加属性
我正在以大致相同的方式创建 API Hanselman 表明它可以完成堆栈溢出。我有一堆 EntityObject 实体框架生成的类和一个 DataService 来将它们序列化为 Atom 和 JSON。我想通过网络服务公开一些生成的属性。将 FullName 视为通过连接 FirstName 和 LastName 生成的(但有些更复杂)。我已将它们添加到扩展实体框架 EntityObject 的分部类中,并为它们提供了 [DataMember]
属性,但它们没有显示在服务中。这是一个示例属性(set
是为了更好的测量而引入的,没有它也不起作用):
[DataMember]
public string FullName
{
get
{
return (this.FirstName ?? "") + " " + (this.LastName ?? "");
}
set { }
}
根据 这些 MSDN 论坛上的讨论,这是一个已知问题。有没有人找到好的解决方法或者有没有人有替代方案的建议?
I'm on the process of creating an API in much the same way Hanselman showed it could be done for Stackoverflow. I have a bunch EntityObject
Entity Framework generated classes and a DataService
thingy to serialize them to Atom and JSON. I would like to expose some generated properties via the web service. Think FullName as generated by concatenating First- and LastName (but some are more complex). I have added these to a partial class extending the Entity Framework EntityObject and given them the [DataMember]
attribute, yet they don't show up in the service. Here's an example attribute (set
is thrown in for good measure, doesn't work without it either):
[DataMember]
public string FullName
{
get
{
return (this.FirstName ?? "") + " " + (this.LastName ?? "");
}
set { }
}
According to these discussions on MSDN forums, this is a known issue. Has anyone found good workarounds or does anyone have suggestions for alternatives?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在通过 WCF 服务公开实体对象时遇到了同样的问题,并使用了您链接到的解决方法 这里 就是在属性中添加如下属性,强制序列化。
我还没有找到任何“更好”的方法来实现这一点。
例如,给定一个名为 Teacher 的实体,其中包含 Title、Forenames 和 Surname 字段,您可以为 Teacher 添加一个部分类,如下所示:
那么只要您的 WCF 服务接口引用该类,那么额外的属性就会被序列化并可供服务的使用者使用。
例如
I had the same issue exposing Entity objects over a WCF service and used the workaround you linked to here which is to add the following attribute to the properties to force them to be serialized.
I haven't found any 'nicer' ways of getting this working.
For example, given an entity called Teacher with fields Title, Forenames and Surname you can add a partial class for Teacher something like:
Then as long as your WCF Service interface references this class then the extra properties are serialised and available for consumers of the service.
e.g.