不在Json对象中创建Breeze导航属性

时间:2014-07-22 12:29:11

标签: breeze

我有一个名为product的产品对象,它具有自引用关系,因此我可以使用包含子产品的父产品。然而,虽然这些是在元数据和查找中创建的,但客户端上的breeze并没有创建它们,它们总是未定义的。

public class Product : BaseEntity
{

    ... properties removed ...
public virtual ICollection<Product> PackContents { get; set; }
public virtual ICollection<Product> PackParents { get; set; }
}

流利配置:

        HasMany(a => a.PackContents).WithMany(b => b.PackParents).Map(m =>
        {
            m.ToTable("PackProducts");
            m.MapLeftKey("ParentId");
            m.MapRightKey("ChildId");
        });

以下是它的微风元数据:

name&#34;:&#34; Product&#34;, &#34; customannotation:ClrType&#34;:&#34; Product,Core,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null&#34;, &#34; key&#34;:{

"propertyRef": {
    "name": "Id"
}

}, &#34;财产&#34;:[  ... ] &#34; navigationProperty&#34;:[

{
    "name": "PackContents",
    "relationship": "Self.Product_PackContents",
    "fromRole": "Product_PackContents_Source",
    "toRole": "Product_PackContents_Target"
},
{
    "name": "PackParents",
    "relationship": "Self.Product_PackContents",
    "fromRole": "Product_PackContents_Target",
    "toRole": "Product_PackContents_Source"
}, ... removed

当我把它放入一个breeze控制器查询查询时,我得到以下模式(以及适当的PackContents有子产品对象)

&#34;产品&#34;:[     {         &#34; $ id&#34;:&#34; 6&#34;,         &#34; $ type&#34;:&#34; Core.Product,Core&#34;,         &#34; PackContents&#34;:[],         &#34; PackParents&#34;:[],     ......其他人被删除     }]

然而,当我在客户端上执行console.log时,PackContents和PackParents的数组不会出现在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而不是PackContents或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; }
}

with config:

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

所以我现在有导航属性,但它们仍未填充,并且都是空数组

2 个答案:

答案 0 :(得分:1)

Breeze还不支持多对多导航属性 - 您需要为联接表创建一个单独的实体,我很害怕。

请参阅http://www.breezejs.com/documentation/presenting-many-many

答案 1 :(得分:0)

我设法添加了对许多导航属性的支持。

转到breeze javascript 找到名为unwrapInstance

的函数
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;
      }
  }