我正在尝试使用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添加标准?
答案 0 :(得分: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>();