2012-01-13 53 views
1

我创建了一个WCF dataservice类来将查询结果返回给javascript客户端。这里是我的dataservice的伪代码:如何从wcf dataservice返回相关实体

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中,它表示延期的相关实体。

我需要做些什么来让这些相关实体返回给客户端?谢谢!

回答

1

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

相关问题