asp.net-web-api - OData-query is throwing error for $expand query
问题描述
There are two tables "Task" and "Client". Both this tables are related with ClientId (foreign key).
In this query I'm trying to get the client name based on ClientId from Task table with $expand keyword. Below is the query and Entity classes.
OData Query : http://localhost:52484/Task?$expand=Client($select=Name)
public class Task: GeneralTask
{
public Task() { }
public Task(
int clientId,
string title,
)
{
this.Title = title;
this.ClientId = clientId;
}
}
public abstract class GeneralTask
{
protected GeneralTask()
{
}
public string Title { get; set; }
public int ClientId { get; set; }
public virtual Client Client { get; set; }
}
But I get the below error.
Error Message :"The query specified in the URI is not valid. The property 'Client' cannot be used in the $expand query option."
Any help would be appreciated.
解决方案
We need to enable OData Model Bound Attributes which you can do globally with the middle line in the following block(WebApiConfig.cs file)
ODataModelBuilder builder = new ODataConventionModelBuilder();
config.Count().Filter().OrderBy().Expand().Select().MaxTop(null); //new line
builder.EntitySet<DB.Project>("Projects");
推荐阅读
- android - 在 Monkey 工具中检查应用程序启动的时间延迟
- eclipse-rcp - 如何通过选择非焦点部分中的按钮来刷新 2 个部分堆栈中的数据
- java - 在指定名字和/或姓氏时查询以获取所有客户
- swift - iOS 14 小部件:使用列表时小部件不呈现
- javascript - 使用对象值循环数字并将输出推送到数组
- c++ - 为什么要调用基类的赋值运算符?
- mysql - mySql 上的全局 event_scheduler 问题
- amcharts - amCharts 4:是否可以按总计对堆积柱形图的类别进行排序
- python - 使用 Python 在一个简单的隔离林示例中绘制异常
- java - 可执行 Jar 文件无法读取内部 JSON 文件