在我的WebAPI 2 / Entity Framework 6 / OData v4服务中,我有以下简单的控制器:
public class InformationProductController : ODataController
{
GCIMContext db = new GCIMContext();
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
[EnableQuery]
public IQueryable<InformationProduct> Get()
{
return db.InformationProducts;
}
}
我的InformationProduct实体有一组类型为DataEntity
的子实体:
public partial class InformationProduct
{
public InformationProduct()
{
this.AnalyticalMethods = new List<AnalyticalMethod>();
this.DataEntities = new List<DataEntity>();
this.BusinessEntities = new List<BusinessEntity>();
this.SourceTools = new List<SourceTool>();
}
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Nullable<int> Governance_ID { get; set; }
public Nullable<int> PerformanceMetric_ID { get; set; }
public virtual ICollection<AnalyticalMethod> AnalyticalMethods { get; set; }
public virtual ICollection<DataEntity> DataEntities { get; set; }
public virtual Governance Governance { get; set; }
public virtual PerformanceMetric PerformanceMetric { get; set; }
public virtual ICollection<BusinessEntity> BusinessEntities { get; set; }
public virtual ICollection<SourceTool> SourceTools { get; set; }
}
DataEntity又有一组DataSource
类型的子实体:
public partial class DataEntity
{
public DataEntity()
{
this.PerformanceMetrics = new List<PerformanceMetric>();
this.DataAttributes = new List<DataAttribute>();
this.BusinessEntities = new List<BusinessEntity>();
this.DataDeliveryChannels = new List<DataDeliveryChannel>();
this.DataSources = new List<DataSource>();
this.MasterDatas = new List<MasterData>();
this.SourceTools = new List<SourceTool>();
this.SubjectAreas = new List<SubjectArea>();
this.Udms = new List<Udm>();
}
public int ID { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public Nullable<int> InformationProduct_ID { get; set; }
public Nullable<int> BiMeasure_ID { get; set; }
public Nullable<int> BiFact_ID { get; set; }
public Nullable<int> BiDimension_ID { get; set; }
public virtual BiDimension BiDimension { get; set; }
public virtual BiFact BiFact { get; set; }
public virtual BiMeasure BiMeasure { get; set; }
public virtual InformationProduct InformationProduct { get; set; }
public virtual ICollection<PerformanceMetric> PerformanceMetrics { get; set; }
public virtual ICollection<DataAttribute> DataAttributes { get; set; }
public virtual ICollection<BusinessEntity> BusinessEntities { get; set; }
public virtual ICollection<DataDeliveryChannel> DataDeliveryChannels { get; set; }
public virtual ICollection<DataSource> DataSources { get; set; }
public virtual ICollection<MasterData> MasterDatas { get; set; }
public virtual ICollection<SourceTool> SourceTools { get; set; }
public virtual ICollection<SubjectArea> SubjectAreas { get; set; }
public virtual ICollection<Udm> Udms { get; set; }
}
以下OData查询在Fiddler,Postman和任何现代浏览器中运行良好:
GET http://10.0.0.4:8080/InformationProduct?$expand=DataEntities($expand=DataSources)
此查询的结果是:
{
"@odata.context":"http://10.0.0.4:8080/$metadata#InformationProduct","value":[
{
"ID":1,"Name":"ODM Dashboard","Description":"ODM Dashboard","Governance_ID":1,"PerformanceMetric_ID":1,"DataEntities":[
{
"ID":1,"Name":"Data Entity 1","Description":"Data Entity 1","InformationProduct_ID":1,"BiMeasure_ID":null,"BiFact_ID":null,"BiDimension_ID":1,"DataSources":[
{
"ID":40,"Category":"Service Performance","SourceSystemName":"Account Improvement Plan","SourceSystemOwner":null,"SourceSystemLocation":null,"SourceSystemTeam":null,"SourceSystemNetworkSegment":null,"SourceSystemOsType":null,"SourceDatabaseName":null,"SourceDatabaseType":null,"SourceDatabaseVersion":null,"BiFact_ID":null
}
]
}
]
}
]
}
由于我无法找到任何可以表达嵌套$expand
运算符的JavaScript库,我现在转向我的API,并希望它提供InformationModel
集合,同时扩展DataEntities
集合,与其自己展开的DataSources
集合一起出现 - 与上面显示的查询完全相同。
我的问题是:我应该使用什么语法来扩展我的IQueryable结果,以包含DataEntities
及其DataSources
集合?
答案 0 :(得分:1)
如果您使用的是Microsoft.AspNet.OData版本5.7或更高版本,则可以使用AutoExpand
属性为DataEntities
和DataSources
属性添加注释。这将使客户端不需要$expand
:
GET http://10.0.0.4:8080/InformationProduct