0

我正在尝试使用 NHibernate 创建查询并沿多个外键搜索:

当我只搜索一个外键时,以下代码有效:

            ICriteria query = Session.CreateCriteria<TblTeam>()
            .Add<TblTeam>(x => x.FldUrlSafeName == teamName)
            .CreateCriteria<TblTeam>(x => x.TblSportsType)
            .Add<TblSportsType>(x => x.FldUrlSafeName == sportsType);


        return query.UniqueResult<TblTeam>();

我需要搜索两个外键,所以我创建了以下内容:

            ICriteria query = Session.CreateCriteria<TblTeam>()
            .Add<TblTeam>(x => x.FldUrlSafeName == teamName)
            .CreateCriteria<TblTeam>(x => x.TblSportsType)
            .Add<TblSportsType>(x => x.FldUrlSafeName == sportsType)
            .CreateCriteria<TblTeam>(x => x.TblSportsOrganization)
            .Add<TblSportsOrganization>(x => x.FldUrlSafeName == sportsOrganization);

        return query.UniqueResult<TblTeam>();

不幸的是,这给了我以下错误:

“无法解析属性:TblSportsOrganization of:site.Core.TblSportsType”

我将如何让它“退后一步”,以便我可以将标准添加到 TblTeam 而不是 TblSportsType?

4

1 回答 1

1

在与 IRC 中的一些人交谈后,我找到了答案。您需要保留对原始 ICriteria 的引用。这应该是显而易见的。

            ICriteria query = Session.CreateCriteria<TblTeam>();
        query.Add<TblTeam>(x => x.FldUrlSafeName == teamName);

        query.CreateCriteria<TblTeam>(x => x.TblSportsType)
            .Add<TblSportsType>(x => x.FldUrlSafeName == sportsType);

        query.CreateCriteria<TblTeam>(x => x.TblSportsOrganization)
            .Add<TblSportsOrganization>(x => x.FldUrlSafeName == sportsOrganization);

        return query.UniqueResult<TblTeam>();
于 2009-07-27T14:05:32.843 回答