views:

182

answers:

1

Edit: changed class names.

I'm using Fluent NHibernate (v 1.0.0.614) automapping on the following set of classes (where Entity is the base class provided in the S#arp Architecture framework):

public class Car : Entity
{
    public virtual int ModelYear { get; set; }
    public virtual Company Manufacturer { get; set; }
}

public class Sedan : Car
{
    public virtual bool WonSedanOfYear { get; set; }
}

public class Company : Entity
{
    public virtual IList<Sedan> Sedans { get; set; }
}

This results in the following Configuration (as written to hbm.xml):

   <class name="Company" table="Companies">
     <id name="Id" type="System.Int32" unsaved-value="0">
       <column name="`ID`" />
       <generator class="identity" />
     </id>
     <bag cascade="all" inverse="true" name="Sedans" mutable="true">
       <key>
           <column name="`CompanyID`" />
       </key>
       <one-to-many class="Sedan" />
     </bag>
   </class>

   <class name="Car" table="Cars">
     <id name="Id" type="System.Int32" unsaved-value="0">
       <column name="`ID`" />
       <generator class="identity" />
     </id>
     <property name="ModelYear" type="System.Int32">
       <column name="`ModelYear`" />
     </property>
     <many-to-one cascade="save-update" class="Company" name="Manufacturer">
       <column name="`CompanyID`" />
     </many-to-one>
     <joined-subclass name="Sedan">
       <key>
          <column name="`CarID`" />
       </key>
       <property name="WonSedanOfYear" type="System.Boolean">
          <column name="`WonSedanOfYear`" />
       </property>
     </joined-subclass>
   </class>

So far so good! But now comes the ugly part. The generated database tables:

Table: Companies
Columns: ID (PK, int, not null)

Table: Cars
Columns: ID (PK, int, not null)
         ModelYear (int, null)
         CompanyID (FK, int, null)


Table: Sedan
Columns: CarID (PK, FK, int, not null)
         WonSedanOfYear (bit, null)
         CompanyID (FK, int, null)

Instead of one FK for Company, I get two!

How can I ensure I only get one FK for Company? Override the automapping? Put a convention in place? Or is this a bug? Your thoughts are appreciated.

A: 

I feel your pain.

I did the following at this point in time because i was running out of time

public class JoinedSubclassConvention : IJoinedSubclassConvention
{
    public void Apply(IJoinedSubclassInstance instance)
    {
        switch (instance.EntityType.Name)
        {
            case "Business":
                instance.Key.ForeignKey("FK_Business_Customer");
                break;
            case "Person":
                instance.Key.ForeignKey("FK_Person_Customer");
                break;
            case "StaffMember":
                instance.Key.ForeignKey("FK_StaffMember_Customer");
                break;
        }
    }
}
ChrisKolenko