我正在研究一个遗留系统,并引入了流畅的nHibernate,但有一个我似乎无法工作的最后一个映射。
代码:
public class Permit
{
public int PermitId { get; set; }
public char Discipline { get; set; }
public PermitDetails PermitDetails { get; set; }
}
public PermitDetails
{
public int PermitId { get; set; }
}
public class GasPermitDetails : PermitDetails
{
... permit details
}
public class ElectricalPermitDetails : PermitDetails
{
... permit details
}
架构:
*tblPermit*
PermitId, int
Discipline, char
.... some other columns
*tblGas*
PermitId, int
....gasDetails
*tblElectrical*
PermitId, int
....electrical details
如果tblPermit.Discipline
为"G"
,我们需要从tblGas
中获取数据。如果tblPermit.Discipline
为"E"
,我们需要从tblElectrical
中获取数据。我一直在试图解决这个问题,但到目前为止还没有运气。
答案 0 :(得分:0)
在我看来,架构是正确的。您可能需要更具体地了解您遇到的问题,因为我认为所有PermitDetails子类的PermitId都成为相应关系表的外键。
EG。对于纪律“气体”,有关许可证应该有许可证的气体信息。如果tblGas中的PermitID是tblPermit的id的外键,则它具有关系。像这样:
+---------------+ +----------------------+
| | 1 1 | |
| tblPermit |<------->| tblGas |
| | | (for gas discipline) |
| | | |
+---------------+ +----------------------+
| | | |
| {PK} PermitId | | {FK} PermitID | <- ForeignKey to tblPermit.PermitID
| | | |
+---------------+ +----------------------+
您不需要Discipline列来选择正确的数据。您只需要一个在许可证标识符上查找tblElectrical,tblGas的查询。这样,您就可以获得许可证及其信息。
(PS。请原谅我使用UML类图来可视化数据库表,但是crows脚符号在ASCII中转换不好)
答案 1 :(得分:0)
(如果tblPermit.Discipline是“G”我们 需要从tblGas中获取数据。如果 tblPermit.Discipline是我们需要的“E” 从tblElectrical获取数据)。一世 试图弄清楚这一点 想通了,但没有运气 到目前为止。
鉴别器字段是无关紧要的,因为子类型的PK字段无论如何都是FK到它们的超类型表。所以这是一个每子表格映射。