简体   繁体   中英

Fluent NHibernate: Eager load multiple collections in mapping

Given I have the following entity with multiple collection properties...

public class Parent 
{
    public virtual int Id { get; set;}  
    public virtual ICollection<FirstChild> FirstChildren { get; set; }
    public virtual ICollection<SecondChild> SecondChildren { get; set; }
} 

Is there a way I can simultaneously eager load both of these properties using fluent NHibernate? Or simply eager load everything associated to the Parent.

If I have the following as my mapping...

public ParentMapping()
{
    Id(p => p.Id).GeneratedBy.Identity();

    HasMany(p => p.FirstChildren)
        .Table("FirstChildren")
        .KeyColumn("Id")
        .Inverse()
        .Cascade.AllDeleteOrphan()
        .Fetch.Join();

    HasMany(p => p.SecondChildren)
        .Table("SecondChildren")
        .KeyColumn("Id")
        .Inverse()
        .Cascade.AllDeleteOrphan()
        .Fetch.Join();
}

The mapping above results in the error:

'Cannot simultaneously fetch multiple bags'.

Using the Fetch.Join() in the mapping works if I use it on just one of the properties.

I am able to eager load everything by using ToFuture() queries, however, I would prefer to do this in the mapping.

You need to use ISet instead of ICollection to use that feature.

You can take a look here and here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM