我有两个类(它们在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 ms - Euro2012 - 200 - / docs / teams / 34
找到关键'团队/ 34'的文件
发给服务器的第一个请求如下所示:
请求#1 648:GET - 5 ms - Euro2012 - 200 - / indices / dynamic / Games?query =&amp; start = 0&amp; pageSize = 128&amp; aggregation = None&amp; sort = GameTime&amp; include = Id(teams / )
所以看起来团队应该被“包括”
我做错了吗?
答案 0 :(得分:3)
您要包含错误的ID,您需要:
.Customize(c => c.Include<Game, Team>(i => i.AwayTeamId ))
.Customize(c => c.Include<Game, Team>(i => i.HomeTeamId ))
答案 1 :(得分:2)
我不熟悉查询的Customize()部分。您可以单独执行Include()。这里有一个例子:
http://inaspiralarray.blogspot.com/2012/03/keeping-domain-model-pure-with-ravendb.html
Include()是一个IRavenQueryable,它位于Raven.Client.Lightweight程序集中。