【问题标题】:Breeze expanded properties - how to reference in controller arraysBreeze 扩展属性 - 如何在控制器数组中引用
【发布时间】:2014-09-02 03:15:54
【问题描述】:

在我第一次尝试使用 Breeze 和 EF6(Hottowel/John Papa 风格)为实体提取完整实体图时,我错过了一个关键的理解

我有Policies-P_Locations-P_Hazards的父子孙关系

我想查看一个客户的所有政策的图表

简而言之,我的查询返回我期望的 JSON 形状(在 XHR 查看器中确认),并且 data.results 允许我向下钻取(通过 backingstore - 伙计,我们是否需要像 C# 这样的 javascript 上的查看器)查看数组儿孙。我看到了我期望客户端的所有策略、位置和危险(帖子末尾的 dll 版本和模型/模型映射代码)

数据上下文:

    function getPoliciesByClientsId(clientsid) {

        var policies;

        return EntityQuery.from('Policies')
            .withParameters({ clientsid: clientsid })
            .expand('P_Locations, P_Locations.P_Hazards')
            .using(manager).execute()
            .then(querySucceeded, _queryFailed);

        function querySucceeded(data) {
            policies = data.results;
            log('Retrieved [Policy] from remote data source', policies.length, true);

            return policies;
        }

    }

控制器:

function getPoliciesByClientsId(clientsid) {

    return datacontext.getPoliciesByClientsId(clientsid).then(function(data) {
        vm.Policies = [];
        vm.Policies = data;
// at this point vm.Policies shows an object array 
// and I can see p_Locations for each policy and p_Hazards for each location
// but vm.Policies.p_locations is undefined

            return vm.Policies;
        }
    );


}

如果我深入研究 datacontext 中的策略或控制器中的 vm.Policies,我会看到驼峰式数组。

但是

vm.Policies.p_Locations 未定义。当然我也想引用 vm.Policies.p_Locations.p_Hazards。

为了引用数据图进行绑定,我缺少什么?

BreezeController:

[HttpGet]
public IQueryable<Policy> Policies()
{
    return _repository.Policies;
}

PwiRepository

    public IQueryable<Policy> Policies
    {
        get { return Context.Policies; }
    }

这就产生了这个请求:

Request URL:http://localhost:29130/breeze/Breeze/Policies?$expand=P_Locations%2CP_Locations%2FP_Hazards&clients_id=439

使用 ma​​nager.metadataStore.getEntityType('P_Location') 然后钻到 导航属性 我找到了这些

navigationProperties: Array[4]0: 

dassociationName: "P_Hazard_P_Location"
entityType: l
entityTypeName: "P_Hazard:#Pwi.Model.Models"
foreignKeyNames: Array[0]
foreignKeyNamesOnServer: Array[0]
invForeignKeyNames: Array[1]
invForeignKeyNamesOnServer: Array[1]
inverse: disScalar: falsename: "p_Hazards"
nameOnServer: "P_Hazards"
parentType: lvalidators: Array[0]

associationName: "P_Location_Policy"
entityType: lentityTypeName: "Policy:#Pwi.Model.Models"
foreignKeyNames: Array[1]
foreignKeyNamesOnServer: Array[1]
invForeignKeyNames: Array[0]
invForeignKeyNamesOnServer: Array[0]
inverse: disScalar: truename: "policy"
nameOnServer: "Policy"parentType: l
relatedDataProperties: Array[1]validators: Array[0]__proto__: Objectlength: 4

这里是dll 件

<packages>
  <package id="Breeze.Server.ContextProvider" version="1.4.17" targetFramework="net45" />
  <package id="Breeze.Server.ContextProvider.EF6" version="1.4.17" targetFramework="net45" />
  <package id="Breeze.Server.WebApi2" version="1.4.17" targetFramework="net45" />
  <package id="EntityFramework" version="6.1.1" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Client" version="5.2.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.Core" version="5.2.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.OData" version="5.2.0" targetFramework="net45" />
  <package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.0" targetFramework="net45" />
  <package id="Microsoft.Data.Edm" version="5.6.2" targetFramework="net45" />
  <package id="Microsoft.Data.OData" version="5.6.2" targetFramework="net45" />
  <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net45" />
  <package id="Newtonsoft.Json" version="6.0.4.0" targetFramework="net45" />
  <package id="System.Spatial" version="5.6.2" targetFramework="net45" />
  <package id="WebActivator" version="1.5.3" targetFramework="net45" />
</packages>

我有 Policies-P_Locations-P_Hazards 的父子孙关系 下面是模型和模型映射的相关部分

P_Location 模型:

public partial class P_Location
    {
        public P_Location()   
        {
            this.P_GlCoverages = new List<P_GlCoverage>();
            this.P_Hazards = new List<P_Hazard>();
            this.P_PropertyCoverages = new List<P_PropertyCoverage>();
        }
        public int P_Locations_id { get; set; }
        public int Policies_id { get; set; }
      ......

      public virtual Policy Policy { get; set; }
      public virtual ICollection<P_Hazard> P_Hazards { get; set; }

P_LocationMap

 // Relationships
            this.HasRequired(t => t.Policy)
                .WithMany(t => t.P_Locations)
                .HasForeignKey(d => d.Policies_id);

P_危险模型

public Policy Policy { get; set; }
        public P_Location P_Location { get; set; }

P_HazardMap

  // Relationships
            this.HasRequired(t => t.P_Location)
                .WithMany(t => t.P_Hazards)
                .HasForeignKey(d => d.P_Locations_id);

【问题讨论】:

    标签: javascript angularjs breeze hottowel


    【解决方案1】:

    首先,我会检查以确保您的查询返回的对象实际上是实体。即他们每个人都有一个'entityAspect'属性吗?如果没有,您将返回投影而不是实体。这可能由于多种原因而发生,但通常是由于命名约定问题或您的 EntityQuery 上缺少“toType”调用。 (如果查询端点不在实体类型/资源名称映射中,则需要这种情况,参数化查询通常是这种情况)。

    我还会使用 getEntityType 查看 MetadataStore,并确保每个实体的属性名称符合您的预期。

    您还可以发布服务器端查询吗?

    【讨论】:

    • 进入孙子的每个实体似乎都有一个 entityAspect 属性。如果我深入研究 p_Locations entityAspect.entityManager 我会看到 metadataStore: i _ctorRegistry: Object _deferredTypes: Object _entityTypeResourceMap: Object ... P_Hazard:#Pwi.Model.Models: "P_Hazards" P_Location:#Pwi.Model.Models: "P_Locations" 。 .. Policy:#Pwi.Model.Models: "Policies" proto: 我期望的模型对象。在原始问题中发布上面的服务器端代码。
    • 我不确定我是否真的在乎,但谁能告诉我谁否决了这个问题以及为什么?我认为它彻底解释了这个问题,并直接解决了 Jay 的要求,我发布了更多信息。
    • 好的,重新阅读您的帖子,当您说“查看所有政策、位置和危险”但“Policies.p_Locations 未定义”时,我不确定我是否理解。你怎么能两者兼得?您在哪里“看到”政策、地点和危害?此外,您应该访问“then”承诺中的“data.results”属性,而不仅仅是“data”。
    • 不确定是谁给你打分,但我刚刚给你打分。
    • 谢谢杰。我的意思是在 JSON 中,我看到所有预期的数据,在 datacontext 中的 data.results 中,我可以钻取策略并查看 p_Locations 数组,然后钻取每个 p_Location 以查看 p_Hazards 数组。在每种情况下,我通过钻取到 _backingstore 来看到这些。对于在控制器中接收数据的 vm.Policies 进行钻取也是如此。我的期望是 vm.Policies.p_Locations 将是一个可绑定的数组。我需要在某个地方明确声明 vm.Policies.p_Locations = [] 吗?
    猜你喜欢
    • 2012-12-06
    • 2016-02-13
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多