我尝试将NH 3.3.1.4000升级到最新版本的NH 4.0.2.4000,我遇到了FetchMany和ThenFetchMany的问题。
在这篇文章中,我了解到这个旧功能不再有效,Breaking changes with NHibernate 4 upgrade。
在新的NH版本上进行此类提取的正确方法是什么?
代码示例:
var IdsList = new List { /* Some Ids */ };
session.Query<A>()
.FetchMany(x=>x.B_ObjectsList)
.ThanFetchMany(x=>x.C_ObjectsList)
.Where(x=>IdsList.Contains(x=>x.Id))
.ToList();
类:
Public Class A
{
public int Id {get;set;}
public IList<B> B_ObjectsList{get;set;}
}
Public Class B
{
public int Id {get;set;}
public IList<C> C_ObjectsList {get;set;}
}
Public Class C
{
public int Id {get;set;}
}
映射:
<class name="A" table="A">
<id name="Id" type="int" column="Id" unsaved-value="0">
<generator class="identity" />
</id>
<bag name="B" table="B" inverse="false" lazy="true"
cascade="all-delete-orphan">
</class>
<class name="B" table="B">
<id name="Id" type="int" column="Id" unsaved-value="0">
<generator class="identity" />
</id>
<bag name="C" table="C" inverse="false" lazy="true"
cascade="all-delete-orphan">
</class>
<class name="C" table="C">
<id name="Id" type="int" column="Id" unsaved-value="0">
<generator class="identity" />
</id>
</class>
答案 0 :(得分:1)
可能
var IdsList = new List { /* Some Ids */ };
var results = session.Query<A>()
.FetchMany(x => x.B_ObjectsList)
.Where(x=>IdsList.Contains(x.Id))
.ToList();
// initialize C_ObjectsList
var bIds = results.SelectMany(x => x.B_ObjectsList).Select(b => b.Id).Distinct().ToList();
session.Query<B>()
.FetchMany(x => x.C_ObjectsList)
.Where(b => bIds.Contains(b.Id))
.ToList();
return results;
答案 1 :(得分:0)
如果B有A的引用,你可以这样做:
var IdsList = new List { /* Some Ids */ };
var results = session.Query<A>()
.Fetch(a => a.B_ObjectsList)
.Where(a => IdsList.Contains(a.Id))
.ToList();
// initialize C_ObjectsList
var aQuery = session.Query<A>()
.Where(x => IdsList.Contains(x.Id));
session.Query<B>()
.Fetch(b => b.C_ObjectsList)
.Where(b => aQuery.Contains(b.A)
.Prefetch();
这样做的好处是不受DB的最大参数限制,在SQL服务器中默认为2100。
而不是ToList()
,我使用此扩展方法:
static public void Prefetch<T>(this IQueryable<T> query)
{
// ReSharper disable once ReturnValueOfPureMethodIsNotUsed
query.AsEnumerable().FirstOrDefault();
}