具有多个连接的Nhibernate查询失败

时间:2011-12-03 16:01:57

标签: nhibernate queryover

我有这样的nhibernate查询:

var query = Session.QueryOver<Immobile>()
                                .WhereRestrictionOn(i => i.Agenzia.CodiceAgenzia).IsLike(codiceAgenzia)
                                .WhereRestrictionOn(i => i.StatoImmobile.StatoImmobileId).IsLike(statoId)
                                .And(i => i.Prezzo <= prezzo)
                                .And(i => i.Mq <= metriquadri);

代码编译但在执行时我收到此异常:

could not resolve property: Agenzia.CodiceAgenzia of: Domain.Model.Immobile

我做错了什么?

1 个答案:

答案 0 :(得分:1)

不幸的是,在你需要先加入它们然后添加限制的Referenced对象上,QueryOver语法不会那样工作..

将代码更改为:

Azengia azengiaAlias=null;   //Azengia here is typeof(Immobile.Azengia) I am assuming it is Azengia
StatoImmobile statoImmobileAlias=null;  //similarly StatoImmobile is assumed to be typeof(Immobile.StatoImmobile)
var query=Session.QueryOver<Immobile>()
.Where(i => i.Prezzo <= prezzo && i.Mq <= metriquadri)
.Inner.JoinAlias(x=>x.Agenzia,()=>azengiaAlias)
.Inner.JoinAlias(x=>x.StatoImmobile,()=.statoImmobileAlias)
.WhereRestrictionOn(() => azengiaAlias.CodiceAgenzia).IsLike(codiceAgenzia)
.WhereRestrictionOn(() => statoImmobileAlias.StatoImmobileId).IsLike(statoId);

希望这有帮助。