【问题标题】:Breeze navigation properties微风导航属性
【发布时间】:2014-01-19 01:59:22
【问题描述】:

我刚刚开始使用 Breeze(使用 Angular)并设法从我的 Odata 服务中获取一些数据并将其显示在页面上。但是,我现在需要通过外键获取相关字段(我需要获取与 Team 对象相关的 Group)并且我遇到了一些困难。在 1.0.3 版之后使用 datajs 会导致问题,所以我使用的是 1.0.3 版,但是这个版本不允许使用“扩展”。因此,我在模型中禁用了延迟加载,并在进入控制器时使用了 Include:

public partial class Team
    {
        public int TeamId { get; set; }
        public string TeamName { get; set; }
        public string FlagSmall { get; set; }
        public string FlagLarge { get; set; }
        public string BadgeSmall { get; set; }
        public string BadgeLarge { get; set; }
        public string TeamImage { get; set; }

        //foreign key
        public int GroupId { get; set; }
        //navigation properties
        public Group Group { get; set; }  //virtual removed to disable lazy loading
    }

    // GET odata/Teams 
            [Queryable]
            public IQueryable<Team> GetTeams()
            {
                return db.Teams.Include("Groups");   //get navigation property
            }

            // GET odata/Teams(5)
            [Queryable]
            public SingleResult<Team> GetTeam([FromODataUri] int key)
            {
                return SingleResult.Create(db.Teams.Include("Groups").Where(team => team.TeamId == key));
            }

但是,这也不起作用,它给出了与> datajs 1.0.3 引起的错误类似的错误:

[Q] Unhandled rejection reasons (should be empty): ["createError@http://bras...s/datajs-1.0.3.js:1055\n"]

有没有人解决这个问题并设法通过外键获取相关字段?

编辑:

这是我的 Angular 代码,我尝试引用 team.Group.GroupName、team.GroupName 和 group.GroupName,但这些都不起作用:

<tbody>
    <tr ng-repeat="team in teams | filter:search:TeamName | orderBy:'TeamId'" id="team_{{team.id}}">
        <td>{{team.TeamId}}</td>
        <td>{{team.TeamName}}</td>
        <td><img src={{team.FlagSmall}}></img></td>
        <td><img style="width:40px;height:40px" src={{team.FlagLarge}}></img></td>
        <td><img style="width:40px;height:40px" src={{team.BadgeSmall}}></img></td>
        <td><img style="width:40px;height:40px" src={{team.BadgeLarge}}></img></td>
        <td><img style="width:80px;height:40px" src={{team.TeamImage}}></img></td>
        <td>{{team.Group.GroupName}}</td>
    </tr>
</tbody>

编辑 2:

这是我的微风查询:

this.basicTeamQuery = function () {
    return manager.executeQuery(entityQuery.from("Teams")).to$q();
};

编辑 3:

经过一些试验后,我设法使用以下 Breeze 查询来解决这个问题:

this.basicTeamQuery = function () {
    return manager.executeQuery(entityQuery.from("Teams").
        select("TeamId, TeamName, FlagSmall, FlagLarge, BadgeSmall, BadgeLarge, TeamImage, " +
        "Group.GroupName").expand("Group")).to$q();
};

不确定这是否是执行此操作的正确方法,或者即使它应该工作...不是扩展不应该与 Angular 一起工作吗?

编辑 4:

这是最终的工作查询,不需要上面的选择:

this.basicTeamQuery = function () {
    return manager.executeQuery(entityQuery.from("Teams").
        expand("Group")).to$q();
};

【问题讨论】:

  • 你确定这个错误只会在你出现时才出现。包括导航属性?听起来我的承诺没有兑现……
  • 在我添加上述更改之前(模型的Group字段中包含和删除虚拟关键字),它成功返回了数据
  • 请复制您用来获取数据的微风查询
  • 请参阅上面的编辑 2。我是否需要在查询中向 Groups 表添加连接才能访问相关的 Group 信息?是否有可能在 Breeze 中做到这一点,因为我猜 Breeze 使用 expand 来达到这个目的?
  • 没有选择就无法展开工作?

标签: javascript angularjs asp.net-web-api odata breeze


【解决方案1】:

尝试添加:

    [ForeignKey("GroupId")]
    [InverseProperty("Teams")]
    public Group Group { get; set; }

编辑:

再次检查后,我发现关系与我乍看之下的不同。由于一个组有许多团队,请尝试以另一种方式查询:

        [Queryable]
        public IQueryable<Group> GetGroupsWithTeams()
        {
            return db.Groups.Include("Teams");   //get navigation property
        }

这应该会正确返回所有内容。

【讨论】:

  • 这会返回一个默认的 JSON 错误消息“发生错误”
  • 如何从 Breeze 查询中调用“GetGroupsWithTeams”?
  • return manager.executeQuery(entityQuery.from("GroupsWithTeams")).to$q(); 应该可以工作。
【解决方案2】:

我刚试过,它对我有用,没有任何问题。

我看到您在问题中复制的 sn-p 中有错字。在 Item 类中定义的导航属性的名称是 Group,但您将“Groups”传递给了 Include 函数。如果您仍然遇到问题,您可以通过修复错字和更新来尝试吗?

更新: 如果不使用扩展,您似乎无法获得导航属性。因为,即使在 LINQ 查询中使用了 Include,即使没有展开的 OData URL 也不会发送导航属性的值。

【讨论】:

  • 谢谢,这至少可以防止出现错误消息...我认为我需要引用集合(组)而不是实际的导航属性名称(组)。但是,请参阅我的编辑,因为到目前为止我还无法在我的角度代码中从 Group 中引入 GroupName。
猜你喜欢
  • 2013-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多