WCF OData 服务可以通过单个请求加载相关数据吗?

发布于 2025-01-05 20:58:25 字数 2358 浏览 1 评论 0原文

我有一张具有“自引用关系”的表 - 一个墙上的评论可能是其他评论的子项。这是我的数据模型:

public class UserWall
{
    [Key]
    public int WId { get; set; }

    public Guid UserId { get; set; }

    public string WContent { get; set; }

    public Guid? WriterId { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime WTime { get; set; }

    public int? ParentWId { get; set; }

    public virtual UserWall Parent { get; set; }

    public virtual ICollection<UserWall> Children { get; set; }
}

因此,使用这个 URI /MyService.svc/UserWalls(1)/ 我得到:

<?xml version="1.0" encoding="utf-8"?>
<entry xml:base="http://localhost:1636/CoreService.svc/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
  <id>http://localhost:1636/CoreService.svc/UserWalls(1)</id>
  <category term="ESN.Models.UserWall" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
  <link rel="edit" title="UserWall" href="UserWalls(1)" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Parent" type="application/atom+xml;type=entry" title="Parent" href="UserWalls(1)/Parent" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Children" type="application/atom+xml;type=feed" title="Children" href="UserWalls(1)/Children" />
  <title />
  <updated>2012-02-15T04:07:39Z</updated>
  <author>
    <name />
  </author>
  <content type="application/xml">
    <m:properties>
      <d:WId m:type="Edm.Int32">1</d:WId>
      <d:UserId m:type="Edm.Guid">e735311a-55fd-48cb-bac0-3441ae99ebea</d:UserId>
      <d:WContent>cức</d:WContent>
      <d:WriterId m:type="Edm.Guid">e735311a-55fd-48cb-bac0-3441ae99ebea</d:WriterId>
      <d:WTime m:type="Edm.DateTime">2012-02-15T01:01:00</d:WTime>
      <d:ParentWId m:type="Edm.Int32" m:null="true" />
    </m:properties>
  </content>
</entry>

使用 URI /MyService.svc/UserWalls(1)/Children 我可以得到他们的孩子。

但是我是否可以在没有额外请求的情况下加载子数据?

I have a table that has an "Self referencing relationship" - One Wall comment maybe a child of other. This is my Data Model:

public class UserWall
{
    [Key]
    public int WId { get; set; }

    public Guid UserId { get; set; }

    public string WContent { get; set; }

    public Guid? WriterId { get; set; }

    [DataType(DataType.DateTime)]
    public DateTime WTime { get; set; }

    public int? ParentWId { get; set; }

    public virtual UserWall Parent { get; set; }

    public virtual ICollection<UserWall> Children { get; set; }
}

And so, with this URI /MyService.svc/UserWalls(1)/ I got:

<?xml version="1.0" encoding="utf-8"?>
<entry xml:base="http://localhost:1636/CoreService.svc/" xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml">
  <id>http://localhost:1636/CoreService.svc/UserWalls(1)</id>
  <category term="ESN.Models.UserWall" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
  <link rel="edit" title="UserWall" href="UserWalls(1)" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Parent" type="application/atom+xml;type=entry" title="Parent" href="UserWalls(1)/Parent" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Children" type="application/atom+xml;type=feed" title="Children" href="UserWalls(1)/Children" />
  <title />
  <updated>2012-02-15T04:07:39Z</updated>
  <author>
    <name />
  </author>
  <content type="application/xml">
    <m:properties>
      <d:WId m:type="Edm.Int32">1</d:WId>
      <d:UserId m:type="Edm.Guid">e735311a-55fd-48cb-bac0-3441ae99ebea</d:UserId>
      <d:WContent>cức</d:WContent>
      <d:WriterId m:type="Edm.Guid">e735311a-55fd-48cb-bac0-3441ae99ebea</d:WriterId>
      <d:WTime m:type="Edm.DateTime">2012-02-15T01:01:00</d:WTime>
      <d:ParentWId m:type="Edm.Int32" m:null="true" />
    </m:properties>
  </content>
</entry>

And with the URI /MyService.svc/UserWalls(1)/Children I can get their children.

But is there anyway that I can load the children data without the extra request?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

黎歌 2025-01-12 20:58:25

是的,您应该能够使用 $expand 关键字。
例如:/MyService.svc/UserWalls(1)?$expand=Children

Yes, you should be able to use the $expand keyword.
Ex: /MyService.svc/UserWalls(1)?$expand=Children

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