WCF 数据服务父实体链接

发布于 2024-09-07 04:52:07 字数 488 浏览 7 评论 0原文

我正在学习 WCF 数据服务(无法升级到 RIA),我认为我会得到的主要好处之一是它会为我维护关系并执行延迟加载...

示例:

双向关系:

Order.Items --> OrderItems
OrderItem.Order --> Order

假设我已经有一个引用订单。然后,我通过调用 BeginLoadProperty(order, "OrderItems") 填充其项目。之后,我希望以下情况为真:

order.OrderItems[0].Order == order;

不幸的是 order.OrderItems[0].Order is null...

这种情况支持吗? WCF 数据服务会为您处理吗?或者您还剩下自定义实现吗?

我使用实体框架作为底层数据服务。

谢谢!

I am learning WCF data services (cannot upgrade to RIA) and one of the major benefits I thougth I would get was that it would maintain relations for me and do lazy loads...

Example:

A bidirectional relation:

Order.Items --> OrderItems
OrderItem.Order --> Order

Say I have already a reference to an order. Then I populate its items by calling BeginLoadProperty(order, "OrderItems"). After that I would expect that the following would be true:

order.OrderItems[0].Order == order;

Unfortunatelly order.OrderItems[0].Order is null...

Is this scenario supported? Will it WCF data services handle for you? Or are you left with your custom implementation?

I use Entity Framework as the underlying data service.

Thanks!

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

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

发布评论

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

评论(2

零度℉ 2024-09-14 04:52:07

由于多种原因,WCF 数据服务不执行延迟加载。一是大多数用户希望完全控制何时发出 Web 请求(因为这可能非常昂贵),二是有时这在技术上是不可能的。例如,在 Silverlight 中,所有 HTTP 请求只能通过异步 API 完成,因此无法真正实现延迟加载,因为访问属性是同步操作。
关于您的问题:WCF 数据服务客户端不了解双向关系。它将其视为两种独立的关系。因此它无法为您修复链接。
为此,您可以将双向关系的知识构建到客户端实体中(例如,您的 Orders 属性可以在将项目添加到其中时修复反向链接)。
另一种可能的解决方案是使用更复杂的查询,使用 $expand 查询选项在同一请求中加载父实体。不幸的是,LoadProperty/BeginLoadProperty API 不支持以这种方式扩展查询,您必须自己构建查询。
因此,为了回答您的最后一个问题,您需要自定义实现。

WCF Data Services doesn't do lazy loading for several reasons. One is that most users want complete control of when a web request is made (as it can be very expensive) and second sometimes it's not technically possible. For example in Silverlight all HTTP requests can be done only through asynchronous API and as such lazy load can't really be implemented since accessing a property is a synchronous operation.
As to your question: The WCF Data Services client doesn't know about bidirectional relations. It sees it as two separate relations. So it can't fixup the links for you.
For this to work you could built the knowledge of bidirectional relations into your client side entities (for example your Orders property can fixup the back link when items are added into it).
The other possible solution would be to use more complex queries using the $expand query option to load the parent entity in the same request. Unfortunately the LoadProperty/BeginLoadProperty API doesn't support extending the query this way, you would have to construct the query yourself.
So to answer you last question, you are left with your custom implementation.

仄言 2024-09-14 04:52:07

在 WCF - ADO.NET 实体数据服务中。您可以使用 .Expand() 来包含

var orderItems = dataServiceContext.OrderItems
                .Expand("Order/Customer, Product")
                .Where(oi => oi.Order.Status == 1)
                .ToList();

示例的相关属性。
客户->订单->订单项目 <- 产品

in WCF - ADO.NET Entity Data Service. You can use .Expand() to include related property

var orderItems = dataServiceContext.OrderItems
                .Expand("Order/Customer, Product")
                .Where(oi => oi.Order.Status == 1)
                .ToList();

for sample.
Customer -> Order -> OrderItems <- Product

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