Mapping from multiple tables to one entity

1,134 views
Skip to first unread message

HighTower

unread,
May 7, 2009, 10:57:45 AM5/7/09
to Fluent NHibernate
Hi

I'm quite new to NHibernate and Fluent Nhibernate.
I'm in the process of starting a new project where we have lanugage
support in the database, so that we can translate one article to
different languages.

The structure of the database tables is as follows:

dbo.Recipe
-ID (PK)
-Portions
-CreatedBy
-etc

dbo.RecipeLang
-ID (PK)
-LanguageID (PK)
-Title
-Description
-etc

My entity/domain class looks like this:
Recipe
-ID
-Title
-Description
-Portions
-CreatedBy
-etc

What I'd like to do is to merge the Recipe and RecipeLang tables i to
the Recipe class. As you can see, it contains properties/fields from
both db tables. Is this possible, and if so, how can I do it?

Thanks

James Gregory

unread,
May 7, 2009, 11:04:35 AM5/7/09
to fluent-n...@googlegroups.com
You'd map your Recipe entity as normal, and associate it with the first table. Inside that mapping you'd then use the WithTable method to extend the mapping over the second table.

Something like this:

public class RecipeMap : ClassMap<Recipe>
{
  public class RecipeMap()
  {
    // Recipe table mappings
    Id(x => x.ID);

    // RecipeLang mappings
    WithTable("RecipeLang", m =>
    {
      m.Map(x => x.Title);
    });

NAnicho

unread,
Feb 27, 2014, 5:50:47 AM2/27/14
to fluent-n...@googlegroups.com
Hey James Gregory, 

I just stumbled upon this! You sere are a life saver! Thank you.

Kind Regards,

Anis Ahmed Chohan.
Reply all
Reply to author
Forward
0 new messages