Fluent NHibernate - 具有共享引用的子类
编辑:更改了类名称。
我在以下一组类上使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我感受到你的痛苦。
我此时做了以下操作,因为我已经没有时间了
I feel your pain.
I did the following at this point in time because i was running out of time