【问题标题】:OData $expand produces odd errorOData $expand 产生奇怪的错误
【发布时间】:2014-09-09 10:26:29
【问题描述】:

当我尝试使用带有 $expand 的 OData 查询时,会产生以下错误:

DbIsOfExpression requires an expression argument with a polymorphic result type that is compatible with the type argument.

我不确定我做错了什么,而且 Google 并没有真正透露该错误背后的太多信息。该项目的所有引用都很好,并且所有其他 OData 查询都按预期工作。

以下是相关的数据模型:

public class ViewLink
{
    public string OverrideLinkName { get; set; }

    public string OverrideDescription { get; set; }

    public bool IsActive { get; set; }

    public int ViewLinkID { get; set; }

    public Nullable<int> ViewID { get; set; }

    public Nullable<int> LinkID { get; set; }

    public Nullable<int> CreatedByID { get; set; }

    public virtual View View { get; set; }

    public virtual Link Link { get; set; }
}

public partial class View
{
    public Nullable<int> SchoolID { get; set; }

    public string ViewName { get; set; }

    public int ViewID { get; set; }

    public Nullable<int> ViewTypeID { get; set; }

    public Nullable<int> TeacherID { get; set; }

    public virtual ViewType ViewType { get; set; }

}

public partial class ViewType
{
    public string TypeName { get; set; }

    public Nullable<int> Priority { get; set; }

    public int ViewTypeID { get; set; }
}

当我尝试使用以下 OData 查询时产生错误:

http://apilinkhere.com/api/viewlink?$top=1&amp;$expand=View/ViewType

我需要在某处更改设置吗?我只是想在我做错的事情背后被引导到正确的方向,并了解这里发生的事情。我不想在以后的项目中继续出现这个错误。

如果还需要其他东西,我会尽力制作。

编辑:

我正在使用 Web API OData。

【问题讨论】:

  • 是否关注ASP.NET Web API OData?还有 WCF Data Service 不同,最好在您的问题中指定。
  • 这表明至少一种类型具有您应该明确查询的子类型。

标签: c# entity-framework odata asp.net-web-api-odata


【解决方案1】:

View 和 ViewType 是什么关系?在您的代码中,似乎 ViewType 是 View 的导航属性。如果是这样,查询应该是 ....?$expand=View($expand=ViewType)。

【讨论】:

  • ViewType 是一个导航属性。我尝试了您的查询,但收到以下错误:“Term 'View($expand=ViewType)' 在 $select 或 $expand 表达式中无效。”
  • 对不起,我错过了“?”在答案中。我已经编辑过了。如果还有问题,可以参考aspnet.codeplex.com/SourceControl/latest#Samples/WebApi/OData/…,这里给出了$select和$expand的详细实现和请求URL。
猜你喜欢
  • 2016-03-15
  • 1970-01-01
  • 2012-06-08
  • 1970-01-01
  • 2019-12-02
  • 2021-06-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-16
相关资源
最近更新 更多