【问题标题】:how to return related entities from wcf dataservice如何从 wcf 数据服务返回相关实体
【发布时间】:2012-01-13 20:30:59
【问题描述】:

我创建了一个 WCF 数据服务类来将查询结果返回给 javascript 客户端。这是我的数据服务的伪代码:

public class MyDataService : DataService<MyEntities>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.SetEntitySetAccessRule("*", EntitySetRights.All);
        config.SetServiceOperationAccessRule("MyGetMethod", ServiceOperationRights.All);
        config.DataServiceBehavior.MaxProtocolVersion = DataServicePRotocolVersion.V2;
    }

    [WebGet(UriTemplate = "{SomeID}"]
    public IEnumerable<Models.Customer> MyGetMethod(int? someID)
    {
        if (someID == null) throw new DataServiceException("ID not specified");

        MyEntities context = this.CurrentDataSource;
        context.ContextOptions.LazyLoadingEnabled = false;

        var q = Linq statement which queries for a collection of entities from context

        IEnumerable<Models.Customer> result = q;
        foreach (Models.Customer customer in result)
        {
            if (!customer.Contacts.IsLoaded)
                customer.Contacts.Load();
        }

        return result;
    }
}

来自客户端请求的调用结果为 json。当我在 dataservice 中调试 get 方法时,结果在一个名为 WrappedRelatedEntities 的属性中扩展了特定的相关数据,但在返回给客户端的 json 中,对于它所说的相关实体,它表示延迟。

我需要做什么才能将这些相关实体返回给客户?谢谢!

【问题讨论】:

    标签: json entity-framework wcf-data-services


    【解决方案1】:

    使用 WCF DS 服务,服务器无法强制扩展导航属性。它仅在客户要求时才有效。因此,更改您的服务操作以返回 IQueryable,然后客户端需要将 $expand=NameOfThePropertyToExpand 添加到 URL。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多