WCF数据服务(OData) - 使用外键扩展导航属性
我目前正在编写基于WCF Dataservices Toolkit的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; }
}
由于从2个独立的数据源检索的数据I只想的项的ID存储在权利对象,而不是整个Item对象。
这适用于诸如:Entitlement('1')/ Item之类的查询,服务理解它需要使用ItemId来执行查找。
然而,当我尝试使用下面的URL 权利(“1”)展开项目?$扩张出现问题=项目
项目总回来为空,我明白,这是因为我我没有在权利对象上存储Item,但是无论如何,我可以强制OData以对待投影的方式来对待扩展语句?
我试过Entitlement('1')?$ select = Item,但是这也会返回为null。
任何建议将不胜感激。
要扩展由导航属性(集合)引用的对象,我认为您需要在URI中使用$ links语法。
请参阅Section 3.2"Addressing Links Between Entities" in the OData Protocol URI Conventions doc。
为了能够使用$扩展模块必须具有虚拟关键字对您关联属性
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的查询选项$扩大
权利(“1 ')?$ expand = Item
是的,我们经历了整个规范,不幸的是没有使用支持跨集合/表加入的关系提供者。我一直有意发布我们当时使用的解决方法/修复。 – SCB
嗨@dthorpe问题实际上在于我们没有使用SQL和实体框架。由于数据必须在运行时解决,因此我们需要拦截入站请求并手动获取额外数据。或者我们需要重写表达式树并手动添加展开包装,以便它将被我们的自定义提供程序拾取。 – SCB