Fluent-Nhibernate References and PropertyRef Doing a Select With Lazy Load

耗尽温柔 提交于 2019-12-19 04:06:24

问题


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.

Order Mapping

Id(x => x.Id).GeneratedBy.Native();
References(x => x.SalesPerson)
                        .LazyLoad()
                        .PropertyRef(x => x.Username)
                        .Column("rsm");
Map(x => x.Title);

Order Class

public class Order : BaseEntity
{
    ...
    public virtual User SalesPerson { get; set; }
    public virtual string Title { get; set; }
    ...
}

User Mapping

Id(x => x.Id).GeneratedBy.Native();
Map(x => x.Username).Column("login");

User Class

public class User : BaseEntity
{
     public virtual string Username { get; set; }
     ...
}

Generated Order Mapping

<many-to-one class="Project.User" lazy="proxy" name="SalesPerson" property-ref="Username">
      <column name="rsm" />
</many-to-one>

Executing Code

var order = session.Get<Order>(1);
Console.WriteLine(order.Title);

Is there anyway to prevent the Select to load the User entity when I'm not using the User entity?


回答1:


Has to do with property-ref see NHibernate creates proxy via session.Load(), but not via Linq or Criteria API

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




回答2:


I don't think this is a bug in NHibernate. It depends on your mapping.

First, remember that the reference map will join the key (primary key and foreign key) between 2 mapping tables. To prevent SELECT + 1, just ignore the key joint.

References(x => x.SalesPerson)
                    .LazyLoad()
                    .PropertyRef(x => x.Username)
                    WithForeignKeyName("none") //disable key join.


来源:https://stackoverflow.com/questions/4888140/fluent-nhibernate-references-and-propertyref-doing-a-select-with-lazy-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!