如何使用 NHibernate 的 Session.Criteria 和多个外键?

发布于 2024-07-29 08:42:54 字数 1152 浏览 7 评论 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 添加条件?

I'm trying to create a query using NHibernate and searching along multiple foreign keys:

The following code works when I'm only searching on one of the foreign keys:

            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>();

I need to search on two of the foreign keys so I created the following:

            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>();

Unfortunately, this gives me the following error:

"could not resolve property: TblSportsOrganization of: site.Core.TblSportsType"

How would I have it take a "step back" as it were so I can add criteria to TblTeam and not TblSportsType?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烙印 2024-08-05 08:42:54

在与 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>();

After talking with some people in IRC I've found an answer. You need to hold on to the reference to the original ICriteria. It should have been obvious.

            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>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文