代码之家  ›  专栏  ›  技术社区  ›  Rashack

WCF数据服务父实体链接

  •  1
  • Rashack  · 技术社区  · 14 年前

    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...

    例子:

    双向关系:

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

    假设我已经有一个订单的参考。然后我通过调用 BeginLoadProperty(order, "OrderItems") . 在此之后,我希望以下内容是正确的:

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

    不幸地 order.OrderItems[0].Order 是空的…

    是否支持此方案?它会为您处理WCF数据服务吗?Or are you left with your custom implementation?

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

    谢谢!

    2 回复  |  直到 13 年前
        1
  •  0
  •   Vitek Karas MSFT    14 年前

    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. 它将其视为两个独立的关系。所以它不能为你修正链接。 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. 所以为了回答最后一个问题,您只剩下定制实现了。

        2
  •  0
  •   aifarfa    13 年前

    在wcf-ado.net实体数据服务中。可以使用.expand()包含相关属性

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

    用于样品。 客户-订单-订单项目-产品