Fluent NHibernate - 加入子类ForeignKey Name

发布于 2024-10-12 09:50:52 字数 2165 浏览 17 评论 0原文

我正在考虑迁移到 Fluent NHibernate - 到目前为止我遇到的唯一问题是您无法在连接的子类映射上指定外键名称。

有没有人对此有解决方案或解决方法?

我发现这篇文章 但该建议显然没有添加到代码中。

如果可能的话,我想避免自己自定义代码。

任何帮助都会很棒...

示例:

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()
    {
        Extends<Product>();
    }
}

这会生成类似以下内容的内容:

<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>

请注意,映射 hbm 文件中没有指定外键名称 - 如下所示:

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

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()
    {
        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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生面具三千个 2024-10-19 09:50:52

尝试这样的事情

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

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));
   }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文