Json 如何从wcf数据服务返回相关实体

Json 如何从wcf数据服务返回相关实体,json,entity-framework,wcf-data-services,Json,Entity Framework,Wcf Data Services,我创建了一个WCF dataservice类来将查询结果返回到javascript客户端。以下是我的数据服务的伪代码: public class MyDataService : DataService<MyEntities> { public static void InitializeService(DataServiceConfiguration config) { config.SetEntitySetAccessRule("*", Entity

我创建了一个WCF dataservice类来将查询结果返回到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;
    }
}
公共类MyDataService:DataService { 公共静态void InitializeService(DataServiceConfiguration配置) { config.SetEntitySetAccessRule(“*”,EntitySetRights.All); config.SetServiceOperationAccessRule(“MyGetMethod”,ServiceOperationRights.All); config.DataServiceBehavior.MaxProtocolVersion=DataServicePRotocolVersion.V2; } [WebGet(UriTemplate=“{SomeID}]” 公共IEnumerable MyGetMethod(int?someID) { 如果(someID==null)抛出新的DataServiceException(“未指定ID”); MyEntities上下文=this.CurrentDataSource; context.ContextOptions.LazyLoadingEnabled=false; var q=Linq语句,用于从上下文查询实体集合 i可数结果=q; foreach(结果中的Models.Customer) { 如果(!customer.Contacts.IsLoaded) customer.Contacts.Load(); } 返回结果; } } 来自客户端的调用请求生成json。当我在dataservice中调试get方法时,result在一个名为WrappedRelatedentials的属性中扩展了特定的相关数据,但在返回给客户端的json中,它表示延迟了相关实体


要将这些相关实体返回到客户端,我需要做些什么?谢谢!

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

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