This is a mirror of official site: http://jasper-net.blogspot.com/

Join on tables using Fluent NHibernate

| Sunday, February 20, 2011
I started using Fluent NHibernate about a year ago as I was experimenting with ORM's in general when a good friend introduced me to the framework.
First of all, I felt that learning Fluent NHibernate in the first place wasn't that easy. I eventually found everything I needed on the internet, but for some reason, it wasn't as easy for me as other frameworks i've dealt with in the past.

Enough said, there were some things that I couldn't find how to do on the internet, like how to join two different tables using Fluent NHibernate mappings. I even spotted some people thirsty for answers...
Check out this question on stackoverflow.com that I found as I was looking for the answer myself, and eventually ended up coming back to answer the question myself! :)

I'll show this using a simple example.

public class FormStructure
{
   public virtual Int32 FormId { get; private set; }
   public virtual Int32 FormType { get; set; }
   public virtual FormField FieldId { get; set; }
}

public class FormField
{
   public virtual int FieldId { get; private set; }
   public virtual String FieldName { get; set; }
   public virtual int? FieldType { get; set; }
   public virtual int? DisplayOrder { get; set; }
}

Now, I want to be able to create a query on the FormStructure entity, and have the results ordered by the matching DisplayOrder field in the FormField entity. I also want the DisplayOrder field to be available to me as a property of the FormStructure entity.

In order to accomplish this, I will need to create a Join between these two tables.
The first step is to add the DisplayOrder field as a property in the FormStructure entity :

public virtual int? DisplayOrder { get; set; }


Read more: [DebuggerStepThrough]

Posted via email from Jasper-net

0 comments: