0

I have a situation which seems simple yet I cannot resolve it nor find similar situations resolved, please help :-)

The following 2 classes on the server:

public class Category
{
    [Key]
    public int CategoryId { get; set; }

    [Required]
    [MaxLength(256)]
    public string Name { get; set; }

    public Int16? Order { get; set; }

    [ForeignKey("ParentCategoryId")]
    public Category ParentCategory { get; set; }
    public int? ParentCategoryId { get; set; }

    [ForeignKey("ParentCategory")]
    [InverseProperty("ParentCategory")]
    public List<Category> SubCategories { get; set; }

    [ForeignKey("ProductId")]
    public List<Product> Products { get; set; }
}
public class Product
{
    [Key]
    public int ProductId { get; set; }

    [Required]
    [MaxLength(256)]
    public string Name { get; set; }

    [MaxLength(1024)]
    public string Desc { get; set; }

    public int ShopPrice { get; set; }
    public int WebPrice { get; set; }

    [MaxLength(32768)]
    public string ProductDetails { get; set; }
}

a simple controller method:

    [HttpGet]
    public IQueryable<Category> Categories()
    {
        return _contextProvider.Context.Categories;
    }

when I use the following url:

http://localhost:49733/api/breeze/Categories?$filter=CategoryId%20eq%2010&$expand=Products

I get the following data:

[{"$id":"1","$type":"Photoshwartz.Models.Category, Photoshwartz","CategoryId":10,"Name":"תת קטגוריה .1.1","Order":2,"ParentCategory":null,"ParentCategoryId":1,"SubCategories":null,"Products":[{"$id":"2","$type":"Photoshwartz.Models.Product, Photoshwartz","ProductId":1,"Name":"מוצר 1.1.1","Desc":"תאור המוצר...","ShopPrice":328,"WebPrice":295,"ProductDetails":"abcdef...."},{"$id":"3","$type":"Photoshwartz.Models.Product, Photoshwartz","ProductId":2,"Name":"מוצר 1.1.2","Desc":"תאור המוצר...","ShopPrice":570,"WebPrice":513,"ProductDetails":"abcdef...."},{"$id":"4","$type":"Photoshwartz.Models.Product, Photoshwartz","ProductId":3,"Name":"מוצר 1.1.3","Desc":"תאור המוצר...","ShopPrice":579,"WebPrice":521,"ProductDetails":"abcdef...."},{"$id":"5","$type":"Photoshwartz.Models.Product, Photoshwartz","ProductId":4,"Name":"מוצר 1.1.4","Desc":"תאור המוצר...","ShopPrice":598,"WebPrice":538,"ProductDetails":"abcdef...."},{"$id":"6","$type":"Photoshwartz.Models.Product, Photoshwartz","ProductId":5,"Name":"מוצר 1.1.5","Desc":"תאור המוצר...","ShopPrice":362,"WebPrice":325,"ProductDetails":"abcdef...."},{"$id":"7","$type":"Photoshwartz.Models.Product, Photoshwartz","ProductId":6,"Name":"מוצר 1.1.6","Desc":"תאור המוצר...","ShopPrice":484,"WebPrice":435,"ProductDetails":"abcdef...."},{"$id":"8","$type":"Photoshwartz.Models.Product, Photoshwartz","ProductId":7,"Name":"מוצר 1.1.7","Desc":"תאור המוצר...","ShopPrice":416,"WebPrice":374,"ProductDetails":"abcdef...."},{"$id":"9","$type":"Photoshwartz.Models.Product, Photoshwartz","ProductId":8,"Name":"מוצר 1.1.8","Desc":"תאור המוצר...","ShopPrice":281,"WebPrice":252,"ProductDetails":"abcdef...."},{"$id":"10","$type":"Photoshwartz.Models.Product, Photoshwartz","ProductId":9,"Name":"מוצר 1.1.9","Desc":"תאור המוצר...","ShopPrice":213,"WebPrice":191,"ProductDetails":"abcdef...."}]}]

as you can see, there is a navigation property called 'Products' which contains 9 items. However, when I try to access this data via the following code I get the non-navigation properties (e.g. 'name()' and 'order()'), but 'products' is undefined (or 'Products' or 'products()' or 'Products()'...). I put a breakpoint in the 'querySucceededAppend' to see this in 'data.results[0]'.

            var query = EntityQuery
                .from("Categories")
                .where("categoryId", "==", categoryId)
                .expand("products");
            manager.executeQuery(query).then(querySucceededAppend)

Any suggeestions ?

Thanks !

Elior

4

1 回答 1

0

将您的展开方法更改为“如图所示的产品 -

var query = EntityQuery
                .from("Categories")
                .where("categoryId", "==", categoryId)
                .expand("Products");
            manager.executeQuery(query).then(querySucceededAppend)

当产品被退回时,您将通过类似的方式访问它们

someObservable = ko.observabled(data.results[0])

console.log(someObservable().products());
于 2013-06-24T23:55:51.913 回答