开发者

Fluent NHibernate - joined subclass ForeignKey Name

I am looking at moving to Fluent NHibernate - the only issue I have encounter so far is that you can not specify a foreign key name on a joined sub class mapping.

Has anyone got a solution for this, or a workaround?

I found this post but the suggestion clearly was not added to the code.

I would like to avoid customising the code myself if possible.

Any help would be great...

Example:

public class Product
{
    public string Name { get; set; }
}

public class Hammer : Product
{
    public string Description { get; set; }
}

public class ProductMap : ClassMap<Product, long>
{
    public ProductMap()
    {
        Polymorphism.Implicit();
        Map(x => x.Name);
    }
}

public class HammerMap : SubclassMap<Hammer>
{
    public HammerMap()
    {
开发者_StackOverflow中文版        Extends<Product>();
    }
}

This generates something like:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-access="field.camelcase-underscore" auto-import="false" default-cascade="none" default-lazy="true">
  <class xmlns="urn:nhibernate-mapping-2.2" dynamic-insert="true" dynamic-update="true" mutable="true" polymorphism="implicit" optimistic-lock="version" name="Domain.Product, Domain" table="Product">
    <id name="Id" type="System.Int64">
      <column name="Id" />
      <generator class="native">
        <param name="sequence">ProductId</param>
      </generator>
    </id>
    <property name="Name" type="System.String">
        <column name="Name" />
    </property>
    <joined-subclass name="Domain.Hammer, Domain" table="Hammer">
      <key>
        <column name="Product_Id" />
      </key>
        <property name="Description" type="System.String">
          <column name="Description" />
        </property>
    </joined-subclass>
  </class>
</hibernate-mapping>

Note that there is no foreign key name specified in the mapping hbm file - as in:

<joined-subclass name="Domain.Hammer, Domain" table="Hammer">
   <key column="Product_Id" foreign-key="FK_Hammer_Product"/>
</joined-subclass>


Try something like this

public class JoinedSubclassForeignKeyConvention : IJoinedSubclassConvention
{
   public void Apply(IJoinedSubclassInstance instance)
   {
      instance.Key.ForeignKey(string.Format("FK_{0}_{1}", 
                    instance.EntityType.Name, instance.Type.Name));
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜