【发布时间】:2017-11-03 10:58:24
【问题描述】:
我将 OData 与实体框架一起使用。假设我有以下模型和控制器方法:
public class Model1
{
public int Id { get; set; }
public int Field2 { get; set; }
public int FieldFromOtherService { get; set; }
public Model2 Model2 { get; set; } // Navigation Property
public int Model2Id { get; set; }
}
public class Model2
{
public int Id { get; set; }
public int Field { get; set; }
}
[HttpGet, EnableQuery]
public IQueryable<Model1> Get()
{
return modelRepository.List();
}
Model1 有属性FieldFromOtherService 不是从数据库中获取的——它是从其他服务中检索的。在应用 OData top、skip、expand 和 select 子句后,我需要一种方法来填充此属性。
有没有办法做到这一点?我尝试对IQueryable 进行包装并在评估后调用操作,但是当查询更复杂时它会崩溃。
【问题讨论】:
-
在您的操作中使用
ODataQueryOptions<Model1>作为参数,而不是EnableQuery。然后,执行options.Apply(modelRepository.List(),这将为您提供对象列表,然后您可以foreach或以其他方式设置所需的属性
标签: c# .net entity-framework asp.net-web-api odata