【问题标题】:How to properly extend a WebAPI 2 OData v4 controller to automatically provide related entity collections如何正确扩展 WebAPI 2 OData v4 控制器以自动提供相关实体集合
【发布时间】:2016-04-10 13:28:16
【问题描述】:

在我的 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 集合?

【问题讨论】:

    标签: c# entity-framework odata asp.net-web-api2


    【解决方案1】:

    如果您使用的是Microsoft.AspNet.OData 5.7 或更高版本,则可以使用AutoExpand 属性注释DataEntitiesDataSources 属性。这将使客户端不需要$expand

    GET http://10.0.0.4:8080/InformationProduct
    

    【讨论】:

    • 我应该在 DbContext 中应用 AutoExpend:[AutoExpand] public DbSet DataEntities { get;放; } 还是在其他地方?
    • 在数据模型中的属性上使用AutoExpand;例如,在 InformationProduct 类的 DataEntities 属性上。
    猜你喜欢
    • 1970-01-01
    • 2017-09-01
    • 2016-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-26
    • 2014-12-11
    • 1970-01-01
    相关资源
    最近更新 更多