breezejs:导航属性未添加到实体 [英] breezejs: navigation property not added to entity
问题描述
我已经配置了我的WebAPI ODATA服务(使用5.0.0-rc1进行$ expand和$ select支持),一切似乎工作正常但导航属性。
I've configured my WebAPI ODATA service (using 5.0.0-rc1 for $expand and $select support) and everything seems to work fine but navigation properties.
元数据确实包含我的导航属性(授权时的OpenPositions):
The metadata does contain my navigation property (OpenPositions on Mandate) :
然后我的微风查询如下:
Then my breeze query is the following:
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控制器:
The WebAPI controller :
[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集合。
Nothing spectacular. But although my Mandate entities are coming back in the resultset, they do not have their OpenPositions collection.
作为测试,如果我将 .select(OpenPositions)
添加到我的微风查询中,那么我得到错误:
As a test, if I add .select("OpenPositions")
to my breeze query, then I get an error:
无法找到属性:entityType上的OpenPositions:Mandate:#WebAPINoBreeze.Models
为什么会这样?
query.entityType.NavigationProperties是一个空数组,所以这可能是一个线索......似乎是微风无法从元数据中构建导航属性。
query.entityType.NavigationProperties is an empty array, so that's probably a clue... It seems breeze could not build navigationproperties out of the metadata.
外国钥匙补充说。问题仍然存在:
foreign key added. problem still there:
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)]属性(我认为这是因为生成了模型类。我找到了一种解决方法,现在元数据包含外键MandateId在OpenPositions中授权:
For some reasons the [ForeignKey("Mandate")] attribute was removed at compile time (I think it's because the model class is generated. I've found a workaround and the metadata now contains the foreign key MandateId to Mandate in the OpenPositions :
推荐答案
您必须定义外键,因为Breeze关联需要FK。 http://www.breezejs.com/documentation/navigation-properties
You must define a foreign key as Breeze associations require FKs. http://www.breezejs.com/documentation/navigation-properties
您的双向关联应如下所示:
Your bi-directional association should look like this:
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; }
}
这篇关于breezejs:导航属性未添加到实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!