Search code examples
breeze

Breeze navigation properties not being created in Json object


I have a product object called product which has a self referencing relationship so I can have parent products with child products. However although these are being created in the metadata and the lookup, breeze on the client is not creating them, they are always undefined.

public class Product : BaseEntity
{

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

Fluent configuration:

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

Here is the breeze metadata for it:

name": "Product", "customannotation:ClrType": "Product, Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null", "key": {

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

}, "property": [ ... ], "navigationProperty": [

{
    "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

when I put this into a breeze controller lookups query I get the following schema (and where appropriate PackContents has child product objects)

"Products": [ { "$id": "6", "$type": "Core.Product, Core", "PackContents": [ ], "PackParents": [ ], ... others removed }]

however when I do a console.log on the client the arrays of PackContents and PackParents do not appear in the json object whereas other navigation properties do:

_$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: [],

Does anyone know why the navigation property companyProducts would be created and not PackContents or PackParents? What can I do to ensure these are created on the client?

Edit:

On advice I created a separate join table:

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

which is referenced in the product class thus:

    public virtual ICollection<ProductRelationship> PackContents { get; set; }
    public virtual ICollection<ProductRelationship> PackParents { get; set; } 

So I now have the navigation properties but they are still not populated and are all empty arrays


Solution

  • Breeze doesn't yet support many-to-many navigation properties - you'll need to have a separate entity for the join table, I'm afraid.

    See http://www.breezejs.com/documentation/presenting-many-many