Getting object and child collection in NHibernate using Linq

I am having a problem using Linq to NHibernate to load an object and eagerly load a child collection. The objects look like this:

public class Order
{
    public Guid Id {get; set; }
    public IList<OrderLine> OrderLines {get;set;}
}
public class OrderLine
{
    public Guid Id {get;set;}
    public string Item {get;set;}
}

      

I am trying to load an order with a specific ID and (eagerly) all its child OrderLines using Linq. My request looks like this:

using (var s = _sessionFactory.OpenSession())
using (var tx = s.BeginTransaction())
{
    var order = from o in s.Linq<Order>().Expand("OrderLines")
                where o.Id == id
                select o;
    return order.First();
}

      

However, when I show the order, the property OrderLines

only contains one object - the database definitely has 3. Unusually, if I foreach around order

before returning, I get all 3 children - but that hits the database twice.

I tried changing the use request Single()

instead, but that doesn't work either.

Am I doing something with linq? Or my misuse Expand

?

Thanks in advance,
Simon.

Note. I am using FluentNHibernate Automapping to create my NH Mapping and my database is a Sqlite database (file, not in memory).

+1


a source to share


1 answer


This function seems to have a bug:

FirstOrDefault () breaks FetchType = Linq connection in NHibernate



Take a look at the generated database query, if it has a TOP 1 clause this could be a problem.

Be aware that Linq for NHibernate is still far from finished product, so errors like this may occur.

+2


a source







All Articles