Fluent nhibernate 使用ToFuture延迟加载子集合
我目前正在使用c#with Nhibernate 3.2访问SqlServer数据库,我正在尝试使用multiquery和Futures加载子集合 无论如何,我可以使用Linq to Nhibernate使其正常工作,但是当查看发送到数据库的sql时,它看起来好像除了子集合获取的子对象之外,还加载了所有父对象(就像是急于加载一样)。我很好奇是否有可能将此行为更改为只拉取所需的子对象列 下面是说明此问题的代码示例Fluent nhibernate 使用ToFuture延迟加载子集合,fluent-nhibernate,lazy-loading,parent-child,linq-to-nhibernate,multi-query,Fluent Nhibernate,Lazy Loading,Parent Child,Linq To Nhibernate,Multi Query,我目前正在使用c#with Nhibernate 3.2访问SqlServer数据库,我正在尝试使用multiquery和Futures加载子集合 无论如何,我可以使用Linq to Nhibernate使其正常工作,但是当查看发送到数据库的sql时,它看起来好像除了子集合获取的子对象之外,还加载了所有父对象(就像是急于加载一样)。我很好奇是否有可能将此行为更改为只拉取所需的子对象列 下面是说明此问题的代码示例 public class Parent : Entity {
public class Parent : Entity
{
public virtual string Name { get; set; }
public virtual IList<Child> Children { get; set; }
}
public class Child : Entity
{
public virtual int Age { get; set; }
public virtual string Name { get; set; }
public virtual Parent Parent { get; set; }
}
public class ChildClassMap : ClassMap<Child>
{
public ChildClassMap()
{
Id(x => x.Id,"Id");
Map(x => x.Age);
Map(x => x.Name);
this.References(x => x.Parent).Column("ParentId").ForeignKey("Id");
}
}
public class ParentClassMap : ClassMap<Parent>
{
public ParentClassMap()
{
Id(x => x.Id, "Id");
Map(x => x.Name);
this.HasMany(x => x.Children).KeyColumn("ParentId");
}
}
public class FamilyRepository : NHibernateRepository<Parent>
{
public Parent GetParent(int id)
{
using (var session = this.Session.OpenSession())
{
var parent = session.Query<Parent>()
.Where(p => p.Id == id);
parent.FetchMany(x => x.Children)
.ToFuture();
return parent.ToFuture().SingleOrDefault();
}
}
}
Sql:
sql探查器的输出为:
exec sp_executesql N'
select parent0_.Id as Id3_0_, children1_.Id as Id2_1_, parent0_.Name as Name3_0_, children1_.Age as Age2_1_, children1_.Name as Name2_1_, children1_.ParentId as ParentId2_1_, children1_.ParentId as ParentId0__, children1_.Id as Id0__
from [Parent] parent0_ left outer join [Child] children1_ on parent0_.Id=children1_.ParentId where parent0_.Id=@p0;
select parent0_.Id as Id3_, parent0_.Name as Name3_ from [Parent] parent0_ where parent0_.Id=@p1;
',N'@p0 bigint,@p1 bigint',@p0=1,@p1=1
有人有什么建议吗
谢谢您的时间只需将代码缩短到
public Parent GetParentWithChildrenInitialised(int id)
{
using (var session = SessionFactory.OpenSession())
{
return session.Query<Parent>()
.Where(p => p.Id == id)
.FetchMany(x => x.Children)
.SingleOrDefault();
}
}
public Parent getparentwithchildreninitialized(int-id)
{
使用(var session=SessionFactory.OpenSession())
{
返回会话。查询()
.其中(p=>p.Id==Id)
.FetchMany(x=>x.Children)
.SingleOrDefault();
}
}
我个人认为应该去掉存储库,因为它添加了不必要的抽象,使性能更难调整,ISession已经像一个respository了
更好的选择是使用session.Get(parentId)代码>因为如果需要子级,则使用lvl1/会话缓存或上面的查询
还可以使用sessionfactory创建会话,因为它是线程安全的,而会话则不是
public Parent GetParentWithChildrenInitialised(int id)
{
using (var session = SessionFactory.OpenSession())
{
return session.Query<Parent>()
.Where(p => p.Id == id)
.FetchMany(x => x.Children)
.SingleOrDefault();
}
}
public Parent getparentwithchildreninitialized(int-id)
{
使用(var session=SessionFactory.OpenSession())
{
返回会话。查询()
.其中(p=>p.Id==Id)
.FetchMany(x=>x.Children)
.SingleOrDefault();
}
}
我个人认为应该去掉存储库,因为它添加了不必要的抽象,使性能更难调整,ISession已经像一个respository了
更好的选择是使用session.Get(parentId)代码>因为如果需要子级,则使用lvl1/会话缓存或上面的查询
还可以使用sessionfactory创建会话,因为它是线程安全的,而会话不是
public Parent GetParentWithChildrenInitialised(int id)
{
using (var session = SessionFactory.OpenSession())
{
return session.Query<Parent>()
.Where(p => p.Id == id)
.FetchMany(x => x.Children)
.SingleOrDefault();
}
}