gpt4 book ai didi

ravendb - Include 的正确使用方法

转载 作者:行者123 更新时间:2023-12-01 11:52:32 24 4
gpt4 key购买 nike

我有两个类(它们保存在 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; }
}

我想像这样输出所有带有球队名称、日期和地点的比赛:

Poland - Greece, 2012-06-08 18:00:00 - Warsaw (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));
}

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

Request #1 654: GET - 0 ms - Euro2012 - 200 - /docs/teams/34

Document with key 'teams/34' was found

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

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

所以看起来这些团队应该被“包括”

我做错了什么吗?

最佳答案

我不熟悉查询的 Customize() 部分。您可以自己执行 Include() 。一个例子在这里:

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

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

关于ravendb - Include<TResult, TInclude> 的正确使用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10072152/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com