Fluent-Nhibernate References 和 PropertyRef 使用延迟加载进行选择 [英] Fluent-Nhibernate References and PropertyRef Doing a Select With Lazy Load
问题描述
我使用 PropertyRef
作为我的 References
属性之一.使用 LazyLoad()
它仍然执行 Select 并加载 User
实体,即使我从未命中" SalesPerson 属性.
I am using PropertyRef
for one of my References
properties. With LazyLoad()
it still does a Select and loads the User
entity, even though I never "hit" the SalesPerson property.
订单映射
Id(x => x.Id).GeneratedBy.Native();
References(x => x.SalesPerson)
.LazyLoad()
.PropertyRef(x => x.Username)
.Column("rsm");
Map(x => x.Title);
订单类
public class Order : BaseEntity
{
...
public virtual User SalesPerson { get; set; }
public virtual string Title { get; set; }
...
}
用户映射
Id(x => x.Id).GeneratedBy.Native();
Map(x => x.Username).Column("login");
用户类
public class User : BaseEntity
{
public virtual string Username { get; set; }
...
}
生成的订单映射
<many-to-one class="Project.User" lazy="proxy" name="SalesPerson" property-ref="Username">
<column name="rsm" />
</many-to-one>
执行代码
var order = session.Get<Order>(1);
Console.WriteLine(order.Title);
当我不使用 User
实体时,是否有任何方法可以阻止 Select 加载 User 实体?
Is there anyway to prevent the Select to load the User entity when I'm not using the User
entity?
推荐答案
与 property-ref 有关,请参阅NHibernate 通过会话创建代理.Load(),但不是通过 Linq 或 Criteria API
Has to do with property-ref see NHibernate creates proxy via session.Load(), but not via Linq or Criteria API
并不是你问的,还要考虑到多对一的代理不允许你进行类型检查,如果你子类化用户,见http://ayende.com/Blog/archive/2010/01/28/nhibernate-new-feature-no-proxy-associations.aspx
And not that you asked, but also consider that many-to-one with proxy does not allow you to do type-checking if you subclass User, see http://ayende.com/Blog/archive/2010/01/28/nhibernate-new-feature-no-proxy-associations.aspx
这篇关于Fluent-Nhibernate References 和 PropertyRef 使用延迟加载进行选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!