WCF 数据服务,从实体框架部分类序列化附加属性

发布于 2024-09-18 22:29:39 字数 984 浏览 4 评论 0原文

我正在以大致相同的方式创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尝蛊 2024-09-25 22:29:39

我在通过 WCF 服务公开实体对象时遇到了同样的问题,并使用了您链接到的解决方法 这里 就是在属性中添加如下属性,强制序列化。

[global::System.Runtime.Serialization.DataMemberAttribute()] 

我还没有找到任何“更好”的方法来实现这一点。

例如,给定一个名为 Teacher 的实体,其中包含 Title、Forenames 和 Surname 字段,您可以为 Teacher 添加一个部分类,如下所示:

public partial class Teacher
{
    [global::System.Runtime.Serialization.DataMemberAttribute()] 
    public string FullName
    {
        get { return string.Format("{0} {1} {2}", Title, Forenames, Surname); }
        set { }
    }
}

那么只要您的 WCF 服务接口引用该类,那么额外的属性就会被序列化并可供服务的使用者使用。

例如

[OperationContract]
List<Teacher> GetTeachers();

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.

[global::System.Runtime.Serialization.DataMemberAttribute()] 

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:

public partial class Teacher
{
    [global::System.Runtime.Serialization.DataMemberAttribute()] 
    public string FullName
    {
        get { return string.Format("{0} {1} {2}", Title, Forenames, Surname); }
        set { }
    }
}

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.

[OperationContract]
List<Teacher> GetTeachers();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文