3

我有两个类(它们保存在 RavenDB 中):

public class Game
{
    public int Id { get; set; }
    public int HomeTeamId { get; set; }
    public int AwayTeamId { get; set; }
    public DateTime GameTime { get; set; }
    public string Location { get; set; }
    public int HomeTeamScore { get; set; }
    public int AwayTeamScore { get; set; }
}

public class Team
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? PositionInGroup { get; set; }
    public int? PositionInPlayOffs { get; set; }
    public string Group { get; set; }
}

我想输出所有带有团队名称、日期和位置的游戏,如下所示:

波兰 - 希腊,2012-06-08 18:00:00 - 华沙​​ (POL)

我知道我可以在 Game 实体中包含主队名称和客队名称,但我不想这样做,而是想使用 Include RavenDB 包含功能。

我写了这段代码:

// Print the schedule
var games = session
    .Query<Game>()
    .Customize(c => c.Include<Game, Team>(i => i.Id))
    .OrderBy(x => x.GameTime)
    .ToList();


foreach (var g in games)
{
    var homeTeam = session.Load<Team>(g.HomeTeamId);
    var awayTeam = session.Load<Team>(g.AwayTeamId);
    Console.WriteLine(
        string.Format("{0} - {1}, {2} - {3}",
        homeTeam.Name,
        awayTeam.Name,
        g.GameTime,
        g.Location));
}

如果我理解正确,当我在循环中加载主队和客队时,不应该向服务器发出获取请求?但是当我查看日志时,我可以看到:

请求 #1 654:GET - 0 毫秒 - Euro2012 - 200 - /docs/teams/34

找到带有关键“teams/34”的文档

向服务器发出的第一个请求如下所示:

请求 #1 648:GET - 5 毫秒 - Euro2012 - 200 - /indexes/dynamic/Games?query=&start=0&pageSize=128&aggregation=None&sort=GameTime&include=Id(teams/)

所以看起来团队应该被“包括在内”

难道我做错了什么?

4

2 回答 2

3

我不熟悉查询的 Customize() 部分。你可以自己做 Include() 。这里有一个例子:

http://inaspiralarray.blogspot.com/2012/03/keeping-domain-model-pure-with-ravenb.html

Include() 是一个 IRavenQueryable,它位于 Raven.Client.Lightweight 程序集中。

于 2012-04-09T14:03:15.260 回答
3

您包含错误的 ID,您需要:

.Customize(c => c.Include<Game, Team>(i => i.AwayTeamId ))
.Customize(c => c.Include<Game, Team>(i => i.HomeTeamId ))
于 2012-04-09T18:45:59.890 回答