3

我第一次使用 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 的:FunnelWeb.Model.Comment

我想,问题是Entry对象没有在这里加载。我怎样才能用 Nhibernate 做到这一点?

4

2 回答 2

5

QueryOver 只是 Criteria 的强类型包装器,不允许隐式深度引用。

你必须使用:

session.QueryOver<Comment>()
       .JoinQueryOver(x => x.Entry)
       .Where(x => x.Author == username)

或者您可以Query<>改用 (LINQ),它可以使用您尝试过的语法。

于 2012-06-08T14:08:39.093 回答
2

您需要 JoinAlias 或 JoinQueryOver。我在下面有一个关于如何使用未来查询的示例......

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;
于 2012-06-08T14:18:32.110 回答