我正在尝试使用联合子类策略和FluentNHibernate映射以下域模型。这是我的课程的外观(删除了不需要的部分)
public class Benefit
{
}
public class Leave : Benefit
{
}
public class SeasonTicketLoan : Benefit
{
}
这是我的映射代码
public class BenefitMappings : ClassMap<Benefit>
{
public BenefitMappings()
{
UseUnionSubclassForInheritanceMapping();
}
}
public class LeaveMappings : SubclassMap<Leave>
{
}
public class SeasonTicketLoanMappings : SubclassMap<SeasonTicketLoan>
{
}
当我使用 SchemaExport
为上述映射生成数据库脚本时,我得到一个用于Leave
的表和另一个用于SeasonTicketLoan
的表,但没有用于Benefit
的表。我在这里错过了什么吗?
。我在这里错过了什么吗?
是的,您正在使用映射每个具体类的表(TPC),旨在创建
- 每个类的单独表和
- 父级没有表。
要获得真正深入和清晰的理解,您应该阅读这篇综合文章:
Fluent Nhibernate中的继承映射策略
在哪里可以阅读:
每个混凝土等级的桌子 (TPC)
在 TPC 继承中,继承层次结构中的每个类都有自己的表。继承层次结构掩盖了以下事实:有几个独立的基础表表示每个子类型。
代码片段提取:
// mapping of the TPCBaseEntity base class
public class TPCBaseEntityMap : ClassMap<TPCBaseEntity>
{
public TPCBaseEntityMap()
{
// indicates that this class is the base
// one for the TPC inheritance strategy and that
// the values of its properties should
// be united with the values of derived classes
UseUnionSubclassForInheritanceMapping();
如果我们还希望每个基类都有表,我们需要:
每类型表(TPT)
TPT 是数据库中描述的具有单独表的继承。每个表都提供其他详细信息,这些详细信息描述了基于另一个表(该表的父表)的新类型。
再次提取一些映射片段:
// mapping of the TPTAnimal base class
public class TPTAnimalMap : ClassMap<TPTAnimal>
{
public TPTAnimalMap()
{
// the name of the schema that stores the table corresponding to the type
Schema("dbo");
// the name of the table corresponding to the type
Table("TPT_Animal");
...
// mapping of the TPTHorse class
public class TPTHorseMap : SubclassMap<TPTHorse>
{
public TPTHorseMap()
{
// the name of the schema that stores the table corresponding to the type
Schema("dbo");
// the name of the table corresponding to the type
Table("TPT_Horse");