Search code examples
c#entity-frameworkodataasp.net-web-api2

How to properly extend a WebAPI 2 OData v4 controller to automatically provide related entity collections


In my WebAPI 2 / Entity Framework 6 / OData v4 service, I have the following simple controller:

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

My InformationProduct entity has a collection of child entities of type 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; }
    }

The DataEntity, in turn, has a collection of child entities of type 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; }
    }

The following OData query runs fine in Fiddler, Postman, and any modern browser:

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

The result of this query is:

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

Since I was unable to find any JavaScript libraries, which can express a nested $expand operator, I am now turning to my API, and would like it to supply the InformationModel collection, along with expanded DataEntities collection, which would come along with its own expanded DataSources collection - exactly like in the query shown above.

My question is: What syntax should I use to extend my IQueryable result to include both DataEntities, and its DataSources collection?


Solution

  • If you are using Microsoft.AspNet.OData version 5.7 or greater, than you can annotate the DataEntities and DataSources properties with the AutoExpand attribute. This will make $expand unnecessary on the client:

    GET http://10.0.0.4:8080/InformationProduct