我已经配置了我的WebAPI ODATA服务(为$expand和$select支持使用5.0.0-rc1),并且一切似乎都可以正常工作,但导航属性却不错.
元数据确实包含我的导航属性(有关任务的OpenPositions):
然后我的微风查询如下:
function search() {
var query = breeze.EntityQuery.from("Mandates").expand("OpenPositions").inlineCount();
return manager.executeQuery(query.using(service)).then(function (result) {
logger.info(result);
}).fail(function (error) {
logger.error(error);
});
}
WebAPI控制器:
[Queryable(AllowedQueryOptions= AllowedQueryOptions.All)]
public override IQueryable<Mandate> Get()
{
return new List<Mandate>() { new Mandate() {
Id = 1,
PolicyNumber = "350000000",
OpenPositions = new List<OpenPosition>(){
new OpenPosition(){ Id = 1, Amount = 2300, Mandate_Id = 1 },
new OpenPosition(){ Id = 2, Amount = 2100, Mandate_Id = 1 }
}},
new Mandate() {
Id = 2,
PolicyNumber = "240000000" ,
OpenPositions = new List<OpenPosition>(){
new OpenPosition(){ Id = 3, Amount = 2500, Mandate_Id = 2 },
new OpenPosition(){ Id = 2, Amount = 2100, Mandate_Id = 2}
}
} }.AsQueryable<Mandate>();
}
没什么壮观的.但是,尽管我的Mandate实体返回到结果集中,但是它们没有其OpenPositions集合.
作为测试,如果我将.select(“ OpenPositions”)添加到微风查询中,则会收到错误消息:
无法找到属性:entityType上的OpenPositions:授权:#WebAPINoBreeze.Models
为什么会这样呢?
[编辑]
query.entityType.NavigationProperties是一个空数组,所以这可能是一个线索.似乎微风无法从元数据中构建Navigationproperties.
[编辑]
外键已添加.问题仍然存在:
public class Mandate
{
public int Id { get; set; }
public string PolicyNumber { get; set; }
public EStatus Status { get; set; }
public virtual List<OpenPosition> OpenPositions { get; set; }
}
public class OpenPosition
{
public int Id { get; set; }
public decimal Amount { get; set; }
[ForeignKey("Mandate")]
public int Mandate_Id { get; set; }
}
** [编辑] **
出于某些原因,[ForeignKey(“ Mandate”)]属性在编译时已删除(我认为是因为生成了模型类.我找到了一种解决方法,并且元数据现在在OpenPositions中包含MandateId到Mandate的外键:
解决方法:
您必须定义一个外键,因为Breeze关联需要FK. http://www.breezejs.com/documentation/navigation-properties
[编辑]
您的双向关联应如下所示:
public class Mandate
{
public int Id { get; set; }
public string PolicyNumber { get; set; }
public virtual ICollection<OpenPosition> OpenPositions { get; set; }
}
public class OpenPosition {
public int Id { get; set; }
public decimal Amount { get; set; }
public int MandateId { get; set; }
public Mandate Mandate {get; set; }
}