Unfortunately I am new to NHibernate and therefore easily confused. Hopefully someone with more experience can set me straight here.
My understanding of lazy loading is that data only gets loaded when it is requested. That's not exactly what I am seeing in my demo project and I don't understand why.
I have a Person object:
Code:
public partial class Person
{
....
List<RotationEvent> rotationEvents = new List<RotationEvent>();
public virtual IList<RotationEvent> RotationEvents
{
get { return rotationEvents; }
set { rotationEvent = value.ToList(); }
}
}
The relevant part of the mapping class looks like this
Code:
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Id(x => x.UniqueID);
HasMany(x => x.RotationEvents).LazyLoad().Cascade.All();
...
}
}
All of this works in terms of saving and retrieving data from the database.
I mainly use the Person object to initialize a PersonDTO object, which contains less data. In particular, it doesn't contain the IList of RotationEvents. However, I see a database call to the RotationEvent table in the log file for each Person object when I initialize the PersonDTO object. This makes data access very slow, because there are hundreds of Person objects.
I was expecting to see no calls to the RotationEvent table at all, since initializing the PersonDTO object doesn't access the RotatonEvents property of the Person object (verified by placing a breakpoint on the get).
Am I misunderstanding how lazy loading is supposed to work or am I doing something that forces NHibernate to load the RotationEvents even though I don't use them in this context?
Thank you!