【发布时间】:2015-12-02 12:24:05
【问题描述】:
我对@987654321@ 子句有疑问:
这有效 -> api/Components('XYZ')/Childs
这不是 -> api/Components('XYZ')?$expand=Childs
我使用 EntityFramework(6.1.3) 来映射数据库中的视图(只读),然后基于 EF 实体创建了 OData v3 控制器。它适用于大多数表,但当实体之间的关系不是基于导航属性而是基于 LINQ 查询时,就会出现问题。
这是代码示例:
public class ComponentsController : ODataController
{
....
[EnableQuery]
public IQueryable<Component> GetChilds([FromODataUri] string key)
{
var id = db.Components.First(c => c.Id == key).Identity;
return db.ChildComponents.Where(cc => cc.Identity == id).Select(cc => cc.Component);
}
我无法理解的是,当(...)/Childs 工作正常时,为什么我不能使用$expand=Childs?
编辑1:
这里是 $metadata - 导航属性“Users”适用于 $expand,但“Childs”不适用:
<EntityType Name="Component">
<Key>
<PropertyRef Name="Id"/>
</Key>
<Property Name="Id" Type="Edm.String" Nullable="false"/>
<Property Name="Type" Type="Edm.String"/>
<Property Name="Name" Type="Edm.String"/>
<Property Name="Identity" Type="Edm.String"/>
<NavigationProperty Name="Users" Relationship="HostingDb.Pandora.HostingDb_Pandora_Component_Users_HostingDb_Pandora_ComponentUser_UsersPartner" ToRole="Users" FromRole="UsersPartner"/>
<NavigationProperty Name="Childs" Relationship="HostingDb.Pandora.HostingDb_Pandora_Component_Childs_HostingDb_Pandora_Component_ChildsPartner" ToRole="Childs" FromRole="ChildsPartner"/>
</EntityType>
<Association Name="HostingDb_Pandora_Component_Users_HostingDb_Pandora_ComponentUser_UsersPartner">
<End Type="HostingDb.Pandora.ComponentUser" Role="Users" Multiplicity="*"/>
<End Type="HostingDb.Pandora.Component" Role="UsersPartner" Multiplicity="0..1"/>
</Association>
<Association Name="HostingDb_Pandora_Component_Childs_HostingDb_Pandora_Component_ChildsPartner">
<End Type="HostingDb.Pandora.Component" Role="Childs" Multiplicity="*"/>
<End Type="HostingDb.Pandora.Component" Role="ChildsPartner" Multiplicity="0..1"/>
</Association>
【问题讨论】:
-
已经支持 $expand 很奇怪。请显示元数据好吗?
-
@SamXu 我添加了元数据
-
使用 mvc api.... 你是如何使用它的,angularjs?即什么叫GetChilds.... js是什么样子的。
-
@Seabizkit,客户端站点不相关,因为我收到 HTTP 500 服务器错误
System.NotSupportedException: The specified type member 'Childs' is not supported in LINQ to Entities...我没有使用任何自动客户端 - 我自己输入查询 url,然后在我的浏览器上进行测试。跨度> -
@micmax93 如果你这么说的话......对我来说 api/Components('XYZ')?$expand=Childs 非常适合你将它传递给 api,我明白你在说什么寿...但我相信它是两者的结合,但您似乎最了解。
标签: c# entity-framework asp.net-web-api odata