WCF数据服务(OData) -使用外键展开导航属性



我目前正在编写一个基于WCF数据服务工具包的OData服务。

服务公开了几个对象,下面列出了一个例子。

public class Entitlement : IEntity
{
    #region Implementation of IEntity
    public string Id { get; set; }
    #endregion
    public string ItemId { get; set; }
    [ForeignProperty]
    public Item Item { get; set; }
}
public class Item : IEntity
{
    #region Implementation of IEntity
    public string Id { get; set; }
    #endregion
    public string ItemName { get; set; }  
}

由于数据是从两个独立的数据源中检索的,所以我只想在Entitlement对象中存储Item的Id,而不是整个Item对象。

这适用于如下查询:Entitlement('1')/Item,服务知道它需要使用ItemId来执行查找。

然而,当我尝试使用下面的URL展开项目时,会出现问题权利(' 1 ')?扩大美元=项目

项目总是返回为null,我理解这是因为我没有将项目存储在权利对象上,但是无论如何,我可以强制OData以对待投影的方式对待扩展语句吗?

我已经尝试了Entitlement('1')?$select=Item,但它也返回null

要展开由导航属性(集合)引用的对象,我认为您需要在URI中使用$links语法。

参见OData协议URI约定文档中的3.2节"寻址实体之间的链接"

要能够使用$expand你的模块必须在你的链接属性

上有virtual关键字
public class Entitlement : IEntity
{
    #region Implementation of IEntity
    public string Id { get; set; }
    #endregion
    public string ItemId { get; set; }
    public virtual Item Item { get; set; }
}

这允许你使用oData查询选项$expand

权利(' 1 ')?扩大美元=项目

最新更新