我第一次使用NHibernate,在这一行中它为我抛出异常
var total = session
.QueryOver<Comment>().Where(p => p.Entry.Author == username)
.ToRowCountQuery()
.FutureValue<int>();
var results = session
.QueryOver<Comment>().Where(p => p.Entry.Author == username)
.Fetch(x => x.Entry).Eager()
.OrderBy(x => x.Posted).Desc()
.Skip(skip)
.Take(take)
.List();
例外是
无法解析属性:Entry.Author of:FunnelWeb.Model.Comment
我猜,问题是这里没有加载Entry
对象。我怎么能用Nhibernate来做这个技巧?
答案 0 :(得分:5)
QueryOver只是Criteria的强类型包装器,不允许隐式深层引用。
你必须使用:
session.QueryOver<Comment>()
.JoinQueryOver(x => x.Entry)
.Where(x => x.Author == username)
或者您可以使用Query<>
代替(LINQ),它可以使用您尝试过的语法。
答案 1 :(得分:2)
您需要JoinAlias或JoinQueryOver。我在下面有一个关于如何使用Future查询的例子......
Entry entryAlias = null;
var q = session.QueryOver<Comment>()
.JoinAlias(x => x.Entry, () => entryAlias)
.Where(() => entryAlias.Author == username);
var totalFuture = q.ToRowCountQuery().FutureValue<int>(); //ToRowcountQuery clones the query, we can reuse it for results
var resultsFuture = q
//.Fetch(x => x.Entry).Eager() //already joined
.OrderBy(x => x.Posted).Desc()
.Skip(skip)
.Take(take)
.Future<Comment>();
var results = resultsFuture.ToList(); //both future queries are executed in the same batch
var total = totalFuture.Value;