我的情况看起来很简单,但我无法解决,也没有找到类似情况解决,请帮助: - )
服务器上的以下两个类:
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; }
}
一个简单的控制器方法:
[HttpGet]
public IQueryable<Category> Categories()
{
return _contextProvider.Context.Categories;
}
当我使用以下网址时:
http://localhost:49733/api/breeze/Categories?$filter=CategoryId%20eq%2010&$expand=Products
我得到以下数据:
[{“$ 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“ “$(典型值) e“:”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,”名称“:”מוצר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 ....”}]}]
如您所见,有一个名为“Products”的导航属性,其中包含9个项目。但是,当我尝试通过以下代码访问此数据时,我获得非导航属性(例如'name()'和'order()'),但'products'未定义(或'Products'或'products( )'或'Products()'...)。我在'querySucceededAppend'中设置了一个断点,以便在'data.results [0]'中看到这一点。
var query = EntityQuery
.from("Categories")
.where("categoryId", "==", categoryId)
.expand("products");
manager.executeQuery(query).then(querySucceededAppend)
任何建议?
谢谢!
Elior
答案 0 :(得分: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());