Search code examples
c#nhibernateormfluent-nhibernate

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.


Solution

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

    You can take a look here and here.