在 nHibernate returns 中更深入地获取代理对象
Deeper fetch in nHibernate returns a proxy object
我尝试使用 nHibernate 获取以下数据模型。
作者 -> 作者列表 -> 书籍
AuthorBook 基本上是一个连接 table,它允许作者和书籍之间的多对多映射。
基本上,我正在尝试提出一个允许我的查询(使用一个会话)
拉出作者和与作者关联的书籍列表。
这是我目前为止尝试过的方法。
var author = session.QueryOver<Author>.Where(x => x.Id = AuthorId)
.Fetch(x => x.SomeList).Eager
.Fetch(x => x.AuthorBooks).Eager
.FutureValue();
该查询将 return 作者书籍列表正确,但列表中的实体是代理项(代理作者和代理书)。所以我决定在同一个会话中再做一次获取。
var authorBooks = session.QueryOver<AuthorBook>()
.JoinQueryOver(x => x.Author)
.Where(x => x.Id = AuthorId)
.Fetch(x => x.Book).Eager
.Fetch(x => x.Author).Eager
.Future();
然后,我将把 AuthorBooks 的结果设置为主父对象 ...
var actualAuthor = author.Value; //from the previous query
actualAuthor.AuthorBooks = new HashSet<AuthorBook>(authorBooks.ToList());
但这并没有起到作用,它仍然向我表明 AuthorBooks 中的记录是代理对象......对我遗漏的内容有什么想法吗?
像这样的东西应该一次性获取所有数据:
AuthorBook authorBooks = null;
Book book = null;
session.QueryOver<Author>()
.Where(x => x.Id = AuthorId)
.JoinAlias(a => a.AuthorBooks, () => authorBooks,JoinType.LeftOuterJoin)
.JoinAlias(() => authorBooks.Book, () => book)
.SingleOrDefault();
我尝试使用 nHibernate 获取以下数据模型。
作者 -> 作者列表 -> 书籍
AuthorBook 基本上是一个连接 table,它允许作者和书籍之间的多对多映射。
基本上,我正在尝试提出一个允许我的查询(使用一个会话) 拉出作者和与作者关联的书籍列表。
这是我目前为止尝试过的方法。
var author = session.QueryOver<Author>.Where(x => x.Id = AuthorId)
.Fetch(x => x.SomeList).Eager
.Fetch(x => x.AuthorBooks).Eager
.FutureValue();
该查询将 return 作者书籍列表正确,但列表中的实体是代理项(代理作者和代理书)。所以我决定在同一个会话中再做一次获取。
var authorBooks = session.QueryOver<AuthorBook>()
.JoinQueryOver(x => x.Author)
.Where(x => x.Id = AuthorId)
.Fetch(x => x.Book).Eager
.Fetch(x => x.Author).Eager
.Future();
然后,我将把 AuthorBooks 的结果设置为主父对象 ...
var actualAuthor = author.Value; //from the previous query
actualAuthor.AuthorBooks = new HashSet<AuthorBook>(authorBooks.ToList());
但这并没有起到作用,它仍然向我表明 AuthorBooks 中的记录是代理对象......对我遗漏的内容有什么想法吗?
像这样的东西应该一次性获取所有数据:
AuthorBook authorBooks = null;
Book book = null;
session.QueryOver<Author>()
.Where(x => x.Id = AuthorId)
.JoinAlias(a => a.AuthorBooks, () => authorBooks,JoinType.LeftOuterJoin)
.JoinAlias(() => authorBooks.Book, () => book)
.SingleOrDefault();