如何使用 NHibernate 的 Session.Criteria 和多个外键?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在与 IRC 中的一些人交谈后,我找到了答案。 您需要保留对原始 ICriteria 的引用。 这应该是显而易见的。
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.