【问题标题】:WCF Data Services (OData) - Expand navigation properties using foreign keyWCF 数据服务 (OData) - 使用外键扩展导航属性
【发布时间】:2014-01-29 22:29:32
【问题描述】:

我目前正在编写一个基于 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; }  
}

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

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

但是,当我尝试使用以下 URL 扩展项目时会出现问题 权利('1')?$expand=Item

Item 总是返回为 null,我知道这是因为我没有将 Item 存储在权利对象上,但无论如何我可以强制 OData 以与处理投影相同的方式处理扩展语句吗?

我尝试过 Entitlement('1')?$select=Item 但这也返回为 null。

任何建议将不胜感激。

【问题讨论】:

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


    【解决方案1】:

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

    Section 3.2"Addressing Links Between Entities" in the OData Protocol URI Conventions doc

    【讨论】:

    • 嗨@dthorpe,问题实际上在于我们没有使用SQL和实体框架。因为数据必须在运行时解析,所以我们需要拦截入站请求并手动获取额外的数据。或者我们需要重写表达式树并手动添加扩展包装器,以便我们的自定义提供程序拾取它。
    【解决方案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')?$expand=Item

    【讨论】:

    • 是的,不幸的是,我们浏览了整个规范并没有使用支持跨集合/表连接的关系提供程序。我一直想发布我们当时使用的解决方法/修复。
    猜你喜欢
    • 1970-01-01
    • 2015-02-16
    • 2018-08-30
    • 1970-01-01
    • 2011-10-19
    • 1970-01-01
    • 2017-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多