开发者

Are all NHibernate References LazyLoaded by default?

开发者 https://www.devze.com 2023-01-17 23:28 出处:网络
I have a (fictional) class with Fluent-mapping: public class Customer { public virtual int Id { get; set; }

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号