如何通过odata v2发送分层数据?

时间:2012-04-21 16:53:26

标签: c#-4.0 wcf-data-services odata linqpad

使用WCF Data Services 4.0,我无法返回分层数据。我有一个类Employee,它有一个EquipmentIds集合。那些EquipmentIds正在通过电线丢失。这是我的代码:

public class ODataV2 : DataService<ODataV2Model>
{
    public static void InitializeService(DataServiceConfiguration config)
    {
        config.UseVerboseErrors = true;
        config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
        config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
    }
}
public class ODataV2Model
{
    public ODataV2Model()
    {
        Employees = new List<Employee>{
            new Employee { Id = 1, Name="Doug", EquipmentIds = new List<Equipment> { new Equipment { Id = 1 },new Equipment { Id = 2 } }.AsQueryable()},
            new Employee { Id = 2, Name= "George", EquipmentIds = new List<Equipment> {new Equipment { Id = 3}, new Equipment { Id = 5} }.AsQueryable() }
        }.AsQueryable();
    }
    public IQueryable<Employee> Employees { get; private set; }
    public IQueryable<Equipment> EquipmentIds { get; private set; }
}
[DataServiceKey("Id")]
public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public IQueryable<Equipment> EquipmentIds { get; set; }
}
[DataServiceKey("Id")]
public class Equipment
{
    public int Id { get; set; }
}

当我运行LinqPad时,我得到了这个: Employees with 0 EquipmentIds on them

我应该有两个EquipmentIds集合,计数为2,但我有0.我没有收到错误,但数据永远不会发送到客户端。

我切换到WCF Data Services v 5.0并且它在.NET端成功运行,但我无法使用LinqPad进行查询。有没有办法在v 4.0中工作?

如果没有,有没有办法升级LinqPad以识别odata v3(WCF数据服务5.0)?

1 个答案:

答案 0 :(得分:1)

查询〜/ Employees将只包含Employee实体,而不包含任何导航属性内容。这是为了减少有效载荷大小。如果你真的想要包含一些导航属性,只需指定例如〜/ Employees?$ expand = EquipmentIds。