【问题标题】:breezejs: navigation property not added to entity微风js:导航属性未添加到实体
【发布时间】:2013-08-29 13:24:32
【问题描述】:

我已经配置了我的 WebAPI ODATA 服务(使用 5.0.0-rc1 来获得 $expand 和 $select 支持)并且一切似乎都工作正常,但导航属性。

元数据确实包含我的导航属性(OpenPositions on Mandate):

那么我的微风查询如下:

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"),则会收到错误消息:

unable to locate property: OpenPositions on entityType: Mandate:#WebAPINoBreeze.Models

为什么会这样?

[编辑] query.entityType.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")] 属性(我认为这是因为生成了模型类。我找到了一种解决方法,元数据现在包含外键 MandateId 以在空缺职位:

【问题讨论】:

    标签: javascript breeze


    【解决方案1】:

    您必须定义一个外键,因为 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; }
    }
    

    【讨论】:

    • 谢谢。我已经添加了外键(请参阅我的帖子的更新),但问题仍然存在。我在这里做错了吗?
    • 看起来你有一个单向关系。您是否使用 Fluent API 进行了正确设置?
    • 我已经完全使用 OpenAccess DataBase First 代码生成器重建了我的模型。我确实有双向关系(Mandate -> OpenPositions 和 OpenPositions -> Mandate)。现在,我不再像上面那样从 IList 返回结果,而是返回 OpenAccess 上下文。然而,breezejs 仍然不知道我的导航属性。对我来说似乎是一个错误。当我使用微风 WebApi 控制器时,我让它工作了。当我停止使用控制器时出现问题。
    • 肯定有别的事情发生了。我已经编辑了显示双向关联应该是什么样子的答案,并且那里应该没有问题。如果您创建一个重现问题的小解决方案,我可以进一步查看。请确保它是一个小型解决方案 - 将其发送至微风支持@ideablade.com。
    • 你的代码看起来和我的差不多。下周我会尝试制作一个复制品。
    猜你喜欢
    • 1970-01-01
    • 2014-01-19
    • 1970-01-01
    • 1970-01-01
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多