Fluent NHibernate - 具有共享引用的子类

发布于 2024-08-20 11:55:38 字数 2195 浏览 5 评论 0原文

编辑:更改了类名称。

我在以下一组类上使用 Fluent NHibernate (v 1.0.0.614) 自动映射(其中 Entity 是 S#arp 架构框架中提供的基类):

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

这会产生以下配置(写入 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>

到目前为止,一切都很好!但现在丑陋的部分来了。生成的数据库表:

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)

我得到了两个,而不是公司的一个 FK!

我如何确保我只为公司获得一份 FK?覆盖自动映射?召开一个会议吗?或者这是一个错误?感谢您的想法。

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.

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

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

发布评论

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

评论(1

〃安静 2024-08-27 11:55:38

我感受到你的痛苦。

我此时做了以下操作,因为我已经没有时间了

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

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