2016-04-10 6 views
0

In meinem WebAPI 2/Entity Framework 6/OData v4 Service zu bieten, habe ich die folgende einfache Steuerung haben:Wie man richtig einen WebAPI 2 OData v4 Controller erweitern automatisch Sammlungen verbundene Einrichtung

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; 
     } 
    } 

Meine InformationProduct Unternehmen hat eine Sammlung von Kindern Einheiten vom Typ 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; } 
    } 

Die DataEntity, wiederum hat eine Sammlung von Kindern Einheiten des Typs 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; } 
    } 

Die folgende OData Abfrage läuft in Fiddler fein, Postbote, und jeder moderne Browser:

GET http://10.0.0.4:8080/InformationProduct?$expand=DataEntities($expand=DataSources) 

Das Ergebnis dieser Abfrage ist:

{ 
    "@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 
      } 
      ] 
     } 
     ] 
    } 
    ] 
} 

Da ich nicht in der Lage war, alle JavaScript zu finden Bibliotheken, die einen verschachtelten Operator $expand ausdrücken können, wende ich mich jetzt an meine API, und möchte es die InformationModel Sammlung, zusammen mit erweiterten DataEntities Sammlung, die mit seinen eigenen e kommen würde liefern xpanded DataSources Sammlung - genau wie in der Abfrage oben gezeigt.

Meine Frage ist: Welche Syntax sollte ich verwenden, um mein IQueryable-Ergebnis so zu erweitern, dass es sowohl DataEntities als auch seine DataSources Sammlung enthält?

Antwort

1

Wenn Sie Microsoft.AspNet.OData Version 5.7 oder höher verwenden, können Sie die Eigenschaften DataEntities und DataSources mit dem Attribut AutoExpand annotieren. Dies wird $expand unnötig auf dem Client machen:

GET http://10.0.0.4:8080/InformationProduct 
+0

sollte ich mich bewerben AutoExpend innerhalb DbContext: [Autoexpand] öffentliche DbSet DataEntities {get; einstellen; } oder an einem anderen Ort? –

+0

Verwenden Sie 'AutoExpand' für Eigenschaften in Ihrem Datenmodell; zum Beispiel für die Eigenschaft 'DataEntities' der Klasse 'InformationProduct'. – lencharest