获取对象时未加载列表

时间:2012-02-13 12:49:37

标签: database nhibernate orm fluent-nhibernate

查看下面的映射。

当我这样做时: session.Get<Customer>(theId);结果返回是正确的客户,但Address列表为空。在数据库中,我看到了客户记录和地址。字段Customer_id中的地址记录(由NHibernate生成)不为空且具有正确的值(客户的ID)。

类和映射

public class Customer
{
    public virtual int Id { get; set; }
    public virtual string LastName { get; set; }
    public virtual Iesi.Collections.Generic.ISet<CustomerAddress> Address { get; set; }

    public Customer()
    {
        Address = new Iesi.Collections.Generic.HashedSet<CustomerAddress>(); 
    }
}

public class CustomerAddress
{
    public virtual int Id { get; set; }
    public virtual string Street { get; set; }
    public virtual Customer Customer { get; set; }
}

public class CustomerMap : ClassMap<Customer>
{
    public CustomerMap()
    {
        Id(x => x.Id).GeneratedBy.Native();
        Map(x => x.LastName)
            .Length(50)
            .Not.Nullable();    
        HasMany(x => x.Address)
            .AsSet()
            .Inverse()
            .Cascade.AllDeleteOrphan();
    }
}

public class CustomerAddressMap : ClassMap<CustomerAddress>
{
    public CustomerAddressMap()
    {
        Id(x => x.Id).GeneratedBy.Native();
        Map(x => x.Street).Length(50);
        References(x => x.Customer);
    }
}

1 个答案:

答案 0 :(得分:3)

默认情况下启用延迟加载,这意味着在触摸Address属性之前,您不会检索地址。您可以使用以下命令禁用映射中的延迟加载:

HasMany(x => x.Address)
        .AsSet()
        .Inverse()
        .Cascade.AllDeleteOrphan()
        .Not.LazyLoad();