Links to Fluent-Nhibernate and PropertyRef Making Lazy Loading Choices

I use PropertyReffor one of my properties References. With LazyLoad()it, it still makes a selection and loads the object User, although I never “hit” the SalesPerson property.

Matching Orders

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 Matching

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

Code execution

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

In any case, to prevent the User object from loading when I am not using the object User?

+3
source share
2 answers

, NHibernate. .

-, , ( ) . SELECT + 1, .

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

Source: https://habr.com/ru/post/1789784/


All Articles