R – How to Map Two Tables To One Class in Fluent NHibernate

fluent-nhibernatenhibernatenhibernate-mapping

I am having a problem with fluent nhiberbate mapping two tables to one class.

I have the following database schema:

TABLE dbo.LocationName
(
  LocationId INT PRIMARY KEY,
  LanguageId INT PRIMARY KEY,
  Name VARCHAR(200)
)

TABLE dbo.Language
(
  LanguageId INT PRIMARY KEY,
  Locale CHAR(5)
)

And want to build the following class definition:

public class LocationName 
{
      public virtual int LocationId { get; private set; }
      public virtual int LanguageId { get; private set; }
      public virtual string Name { get; set; }
      public virtual string Locale { get; set; } 
}

Here is my mapping class:

  public LocalisedNameMap()
  {
     WithTable("LocationName");
     UseCompositeId()
        .WithKeyProperty(x => x.LanguageId)
        .WithKeyProperty(x => x.LocationId);
     Map(x => x.Name);

     WithTable("Language", lang =>
                             {
                                lang.WithKeyColumn("LanguageId");
                                lang.Map(x => x.Locale);
                             });
  }

The problem is with the mapping of the Locale field being from another table, and in particular that the keys between those tables don't match. Whenever I run the application with this mapping I get the following error on startup:

Foreign key
(FK7FC009CCEEA10EEE:Language
[LanguageId])) must have same number
of columns as the referenced primary
key (LocationName [LanguageId,
LocationId])

How do I tell nHibernate to map from LocationName to Language using only the LanguageId field?

Best Answer

You can use , don't know the name in FNH, to denormalize a class, that is, bring in a column from another table. See http://ayende.com/blog/3961/nhibernate-mapping-join