获取对象时未加载列表

时间:2021-02-22 18:21:39

Look the mapping below.

查看下面的映射。

When I do : session.Get<Customer>(theId); The result return is the right customer but the Address list is empty. In the database, I see the customer record and the address. The address record in the field Customer_id (generated by NHibernate) is not null and has the right value (id of the customer).

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

Class and Mapping

类和映射

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 个解决方案

#1


3  

Lazy loading is enabled by default, which means you won't retrieve the addresses until you touch the Address property. You can disable lazy loading in your mapping with:

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

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

#1


3  

Lazy loading is enabled by default, which means you won't retrieve the addresses until you touch the Address property. You can disable lazy loading in your mapping with:

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

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