未在Json对象中创建Breeze导航属性
我有一个名为product的产品对象,它具有自引用关系,因此我可以将父产品与子产品关联起来。然而,尽管这些都是在元数据和查找中创建的,但是客户端上的breeze并没有创建它们,它们始终是未定义的未在Json对象中创建Breeze导航属性,breeze,Breeze,我有一个名为product的产品对象,它具有自引用关系,因此我可以将父产品与子产品关联起来。然而,尽管这些都是在元数据和查找中创建的,但是客户端上的breeze并没有创建它们,它们始终是未定义的 public class Product : BaseEntity { ... properties removed ... public virtual ICollection<Product> PackContents { get; set; } public virtual
public class Product : BaseEntity
{
... properties removed ...
public virtual ICollection<Product> PackContents { get; set; }
public virtual ICollection<Product> PackParents { get; set; }
}
以下是它的基本元数据:
名称“:“产品”,
“customannotation:ClrType”:“产品,核心,版本=1.0.0.0,区域性=中立,PublicKeyToken=null”,
“关键”:{
},,
“财产”:[
...
],
“navigationProperty”:[
当我将其放入breeze控制器查找查询时,我得到了以下模式(在适当的情况下,PackContents具有子产品对象)
“产品”:[
{
“$id”:“6”,
“$type”:“Core.Product,Core”,
“包装内容”:[],
“合同当事人”:[],
…其他已删除
}]
但是,当我在客户端上执行console.log时,packcontent和packparent的数组不会显示在json对象中,而其他导航属性会显示:
_$typeName: "Product:#Core",
_backingStore: Object { id=7, productTypeId=1, description="Vaillant 624", more...},
_pendingBackingStores: [],
brand: null,
code: null,
companyProducts: [],
cost: 750,
description: "Vaillant 624",
entityAspect: Object { entity={...}, entityGroup={...}, entityManager={...}, more...},
entityType: Product:#Core { shortName="Product", namespace="Core", isAbstract=false, more...},
id: 7,
// I am expecting to see Pack Contents and pack parents here
quoteMeasureProducts: [],
quoteProducts: [],
有人知道为什么要创建导航属性companyProducts而不是PackContent或PackParents吗?我该如何确保在客户端上创建这些内容
编辑:
根据建议,我创建了一个单独的联接表:
public class ProductRelationship : BaseEntityInt
{
public int ParentId { get; set; }
public int ChildId { get; set; }
/// <summary>
///
/// </summary>
public virtual Product Parent { get; set; }
public virtual Product Child { get; set; }
}
在产品类别中引用,因此:
public virtual ICollection<ProductRelationship> PackContents { get; set; }
public virtual ICollection<ProductRelationship> PackParents { get; set; }
public虚拟ICollection PackContents{get;set;}
公共虚拟ICollection PackParents{get;set;}
因此,我现在有了导航属性,但它们仍然没有填充,并且都是空数组Breeze还不支持多对多导航属性-恐怕您需要为联接表提供一个单独的实体
请参见我设法添加了对多对多导航属性的支持 转到breeze javascript 查找名为unwapenstance的函数
function unwrapInstance(structObj, transformFn)
换成这个
function unwrapInstance(structObj, transformFn) {
if (!structObj.unwrapped) {
structObj.unwrapped = true;
var rawObject = {};
var stype = structObj.entityType || structObj.complexType;
var serializerFn = getSerializerFn(stype);
var unmapped = {};
stype.dataProperties.forEach(function (dp) {
if (dp.isComplexProperty) {
rawObject[dp.nameOnServer] = __map(structObj.getProperty(dp.name), function (co) {
if (!co.unwrapped) {
return unwrapInstance(co, transformFn);
}
});
} else {
var val = structObj.getProperty(dp.name);
val = transformFn ? transformFn(dp, val) : val;
if (val === undefined) return;
val = serializerFn ? serializerFn(dp, val) : val;
if (val !== undefined) {
if (dp.isUnmapped) {
unmapped[dp.nameOnServer] = __toJSONSafe(val);
} else {
rawObject[dp.nameOnServer] = val;
}
}
}
});
stype.navigationProperties.forEach(function (dp) {
rawObject[dp.nameOnServer] = __map(structObj.getProperty(dp.name), function (co) {
if (!co.unwrapped) {
return unwrapInstance(co, transformFn);
}
});
});
if (!__isEmpty(unmapped)) {
rawObject.__unmapped = unmapped;
}
return rawObject;
}
}
谢谢。这就是PackContent和packParents不存在的原因吗,因为还有其他导航属性不存在,我也无法解释。我看不到其他属性的原因是因为我的Get方法返回了IEnumerable和IEnumerable的投影。我使用了IQueryable来获取围绕这一点,删除了投影。它似乎不喜欢投影。我为此添加了一个轻松的建议。如果您认为这是导航属性的一个好解决方案,请投票
HasRequired(a => a.Child).WithMany(b => b.PackParents).HasForeignKey(c => c.ChildId);
HasRequired(a => a.Parent).WithMany(b => b.PackContents).HasForeignKey(c => c.ParentId);
public virtual ICollection<ProductRelationship> PackContents { get; set; }
public virtual ICollection<ProductRelationship> PackParents { get; set; }
function unwrapInstance(structObj, transformFn)
function unwrapInstance(structObj, transformFn) {
if (!structObj.unwrapped) {
structObj.unwrapped = true;
var rawObject = {};
var stype = structObj.entityType || structObj.complexType;
var serializerFn = getSerializerFn(stype);
var unmapped = {};
stype.dataProperties.forEach(function (dp) {
if (dp.isComplexProperty) {
rawObject[dp.nameOnServer] = __map(structObj.getProperty(dp.name), function (co) {
if (!co.unwrapped) {
return unwrapInstance(co, transformFn);
}
});
} else {
var val = structObj.getProperty(dp.name);
val = transformFn ? transformFn(dp, val) : val;
if (val === undefined) return;
val = serializerFn ? serializerFn(dp, val) : val;
if (val !== undefined) {
if (dp.isUnmapped) {
unmapped[dp.nameOnServer] = __toJSONSafe(val);
} else {
rawObject[dp.nameOnServer] = val;
}
}
}
});
stype.navigationProperties.forEach(function (dp) {
rawObject[dp.nameOnServer] = __map(structObj.getProperty(dp.name), function (co) {
if (!co.unwrapped) {
return unwrapInstance(co, transformFn);
}
});
});
if (!__isEmpty(unmapped)) {
rawObject.__unmapped = unmapped;
}
return rawObject;
}
}