views:

357

answers:

2

Assume I have this domain object...

public class SpansMultipleTables
{
     public int CommonID {get; set;}

     public string Table1Value {get; set;}

     public int Table2Value {get; set;}

     public float Table3Value {get; set;}
}
  • The CommonID property maps to the "ID" column on all tables.
  • The Table1Value property maps to the "Value" column in the table "Table1"
  • The Table2Value property maps to the "Value" column in the table "Table2"
  • The Table3Value property maps to the "Value" column in the table "Table3"

Using FluentNHibernate, how can I set up a map for this object that really doesn't have a central table as it's home?

A: 

I'm not an expert, but I'm not sure if NHibernate can easily handle a mapping like this. If you are able to modify the schema, you might be able to define a "master" table, which just has the CommonID as the primary key. The class for the master table can then map the other tables as properties by their relationship to the master table. Another option might be to designate one of the tables as the master, and have the others mapped as properties on the master class. In any case, it would probably be a good idea to create some FK relationships between these tables to guarantee that the combined "entity" cannot have its parts removed separately.

Andy White
+1  A: 

Try Join, but I would recommend changing your design.

public class SpansMultipleTablesMap : ClassMap<SpansMultipleTables>
{
  public SpansMultipleTablesMap()
  {
    Id(x => x.CommonID);
    Join("Table1", m =>
    {
      m.Map(x => x.Table1Value, "Value");
    });
    Join("Table2", m =>
    {
      m.Map(x => x.Table2Value, "Value");
    });
    Join("Table3", m =>
    {
      m.Map(x => x.Table3Value, "Value");
    });
  }
}
James Gregory
+1 I may be forced to go this route because the senior developer on my team refuses to accept that this is a bad and unmaintainable design.
Jim Schubert

related questions