I have a (fictional) class with Fluent-mapping:
public class Customer
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Employee Responsible { get; set; }
public virtual IList<Order> Orders { get; set; }
}
public class CustomerMapping : ClassMap<Customer
{
public CustomerMapping()
{
Id(c => c.Id);
Map(c => c.Name);
References(c => c.Responsible);
HasMany(c => c.Orders);
}
}
Now - if I fetch a customer from database the HasMany-reference is Lazyloaded, but the References-reference seems not to be lazy loaded. Is this expected? Do I need to explicitly it?
var fromDb = Session.Get<Customer>(id);
Assert.That(!NHibernateUtil.IsInitialized(fromDb.Orders));
Assert.That(!NHibernateUtil.IsInitialized(fromDb.Reponsible)); // <--开发者_高级运维 fails
References (many-to-one) are lazy loaded by default. My bet is that you previously loaded the Responsible object in the same session and it was retrieved from first level cache rather than the database.
精彩评论