NHibernate C#内连接n + 1

时间:2013-09-13 12:13:44

标签: c# sql nhibernate fluent-nhibernate

这是我简单的С#代码:

var chests = _session.CreateCriteria<GameCharacterChest>()
    .CreateAlias("GameCharacterEquipments", "equip", JoinType.InnerJoin)
    .Add(Restrictions.Eq("GameCharacterId", characterId))
    .List<GameCharacterChest>();

foreach (var chest in chests)
{
    var equip = chest.GameCharacterEquipments.FirstOrNull();
    if (equip != null)
    {
        var slot = (equip as GameCharacterEquipment).GameCharacterSlotTypeId;
    } 
}

foreach周期中,NHibernate将SQL查询发送到我的数据库。为什么?内部联接在第一次查询中已经请求GameCharacterEquipment的信息。 我有流利的地图。

1 个答案:

答案 0 :(得分:0)

尝试添加

.SetFetchMode("GameCharacterEquipments", FetchMode.Join)

.SetFetchMode("GameCharacterEquipments", FetchMode.Eager)

各种电话之间(CreateAlias之后)

您可能在GameCharacterEquipments上定义了懒惰关系。