Fluent Nhibernate inner join

lloiacono picture lloiacono · Oct 27, 2011 · Viewed 15.7k times · Source

I have 3 tables (Master, Imagen, Linea) were:

Master
public virtual int Id { get; private set; }
public virtual Imagen imagen { get; set; }

Imagen
public virtual int Id { get; private set; }
public virtual Linea linea { get; set; }

Linea
public virtual int Id { get; private set; }
public virtual String Nombre { get; set; }

I need a query like this:

SELECT * FROM dbo.Master 
INNER JOIN dbo.Imagen ON dbo.Master.imagen_id = dbo.Imagen.Id 
INNER JOIN dbo.Linea ON dbo.Imagen.linea_id = dbo.Linea.Id 
WHERE dbo.Linea_Id = 5

But I dont know how to tell Fluent Nhibernate to create this query using the automapper. So far I've tried this:

ICriteria c = session.CreateCriteria(typeof(Master))
  .CreateAlias("dbo.Imagen", "img", JoinType.InnerJoin)
  .Add(Restrictions.Eq("img.linea_id", id_linea));

return c.List<Master>();

But I get this error: could not resolve property: dbo of: ImageManager.Model.Entity.Master

Any ideas on how to do a Inner Join? Thanks in advance

Answer

Nathan Fisher picture Nathan Fisher · Oct 27, 2011

For a start I would get rid of the dbo from dbo.Imagen. Using the ICriteria interface you need to think in-terms of objects, not database tables, even though there may be a one to one mapping of object to table and properties to columns.

EDIT:
another option would be to use the QueryOver Lambda syntax.

   var list = session.QueryOver<Master>()
                        .JoinQueryOver(master => master.imagen)
                        .Where(imagen => imagen.linea.Id == 5)
                        .List();