How to Map Two Tables To One Class in Fluent NHibernate

Posted by Richard Nagle on Stack Overflow See other posts from Stack Overflow or by Richard Nagle
Published on 2009-06-24T16:40:12Z Indexed on 2012/12/14 23:04 UTC
Read the original article Hit count: 229

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?

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about fluent-nhibernate