【发布时间】: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
使用 manager.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