C# 如何正确扩展WebAPI 2 OData v4控制器以自动提供相关的实体集合
在我的WebAPI 2/Entity Framework 6/OData v4服务中,我有以下简单控制器:C# 如何正确扩展WebAPI 2 OData v4控制器以自动提供相关的实体集合,c#,entity-framework,odata,asp.net-web-api2,C#,Entity Framework,Odata,Asp.net Web Api2,在我的WebAPI 2/Entity Framework 6/OData v4服务中,我有以下简单控制器: public class InformationProductController : ODataController { GCIMContext db = new GCIMContext(); protected override void Dispose(bool disposing) { db.Disp
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;
}
}
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.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
集合,它将与自己扩展的数据源
集合一起出现-与上面显示的查询完全相同
我的问题是:如果使用5.7版或更高版本,我应该使用什么语法来扩展IQueryable结果,以包括数据实体
,及其数据源
集合?如果您使用的是5.7版或更高版本,那么您可以使用该属性注释数据实体
和数据源
属性。这将使客户机上不需要$expand
:
GET http://10.0.0.4:8080/InformationProduct
我应该在DbContext:[AutoExpand]public DbSet DataEntities{get;set;}内或在其他地方应用AutoExpand吗?对数据模型中的属性使用
AutoExpand
;例如,在InformationProduct
类的DataEntities
属性上。
{
"@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
}
]
}
]
}
]
}
GET http://10.0.0.4:8080/InformationProduct