问题描述
鉴于以下情况,我想使用Fluent NHibernate将类型层次结构映射到数据库架构.
Given the following scenario, I want map the type hierarchy to the database schema using Fluent NHibernate.
我正在使用NHibernate 2.0
I am using NHibernate 2.0
类型层次结构
public abstract class Item
{
public virtual int ItemId { get; set; }
public virtual string ItemType { get; set; }
public virtual string FieldA { get; set; }
}
public abstract class SubItem : Item
{
public virtual string FieldB { get; set; }
}
public class ConcreteItemX : SubItem
{
public virtual string FieldC { get; set; }
}
public class ConcreteItemY : Item
{
public virtual string FieldD { get; set; }
}
Item
和SubItem
类是抽象的.
数据库架构
+----------+ +---------------+ +---------------+
| Item | | ConcreteItemX | | ConcreteItemY |
+==========+ +===============+ +===============+
| ItemId | | ItemId | | ItemId |
| ItemType | | FieldC | | FieldD |
| FieldA | +---------------+ +---------------+
| FieldB |
+----------+
ItemType
字段确定具体类型.
ConcreteItemX
表中的每个记录在Item
表中都有一个对应的记录;对于ConcreteItemY
表也是如此.
Each record in the ConcreteItemX
table has a single corresponding record in the Item
table; likewise for the ConcreteItemY
table.
FieldB
始终为null.
映射(到目前为止)
public class ItemMap : ClassMap<Item>
{
public ItemMap()
{
WithTable("Item");
Id(x => x.ItemId, "ItemId");
Map(x => x.FieldA, "FieldA");
JoinedSubClass<ConcreteItemX>("ItemId", MapConcreteItemX);
JoinedSubClass<ConcreteItemY>("ItemId", MapConcreteItemY);
}
private static void MapConcreteItemX(JoinedSubClassPart<ConcreteItemX> part)
{
part.WithTableName("ConcreteItemX");
part.Map(x => x.FieldC, "FieldC");
}
private static void MapConcreteItemY(JoinedSubClassPart<ConcreteItemY> part)
{
part.WithTableName("ConcreteItemX");
part.Map(x => x.FieldD, "FieldD");
}
}
FieldB
未映射.
问题
如何使用Fluent NHibernate映射SubItem
类的FieldB
属性?
How do I map the FieldB
property of the SubItem
class using Fluent NHibernate?
我可以使用ItemType
字段来利用DiscriminateSubClassesOnColumn
吗?
Is there any way I can leverage DiscriminateSubClassesOnColumn
using the ItemType
field?
附录
我可以使用hbm.xml文件获得所需的结果:
I am able to achieve the desired result using an hbm.xml file:
<class name="Item" table="Item">
<id name="ItemId" type="Int32" column="ItemId">
<generator class="native"/>
</id>
<discriminator column="ItemType" type="string"/>
<property name="FieldA" column="FieldA"/>
<subclass name="ConcreteItemX" discriminator-value="ConcreteItemX">
<!-- Note the FieldB mapping here -->
<property name="FieldB" column="FieldB"/>
<join table="ConcreteItemX">
<key column="ItemId"/>
<property name="FieldC" column="FieldC"/>
</join>
</subclass>
<subclass name="ConcreteItemY" discriminator-value="ConcreteItemY">
<join table="ConcreteItemY">
<key column="ItemId"/>
<property name="FieldD" column="FieldD"/>
</join>
</subclass>
</class>
如何使用Fluent NHibernate完成上述映射?
How do I accomplish the above mapping using Fluent NHibernate?
是否可以使用Fluent NHibernate将每个类的表与每个子类的表混合在一起?
Is it possible to mix table-per-class-hierarchy with table-per-subclass using Fluent NHibernate?
推荐答案
我知道这确实很老,但是现在可以很容易地设置fluent以生成您最初想要的精确映射.由于我在寻找答案时碰到了这篇文章,所以我以为会发布它.
I know this is really old, but it is now pretty simple to set up fluent to generate the exact mapping you initially desired. Since I came across this post when searching for the answer, I thought I'd post it.
您只需为基类创建ClassMap即可,而无需引用您的子类:
You just create your ClassMap for the base class without any reference to your subclasses:
public class ItemMap : ClassMap<Item>
{
public ItemMap()
{
this.Table("Item");
this.DiscriminateSubClassesOnColumn("ItemType");
this.Id(x => x.ItemId, "ItemId");
this.Map(x => x.FieldA, "FieldA");
}
}
然后像这样映射您的抽象子类:
Then map your abstract subclass like this:
public class SubItemMap: SubclassMap<SubItemMap>
{
public SubItemMap()
{
this.Map(x => x.FieldB);
}
}
然后像这样映射您的具体子类:
Then map your concrete subclasses like so:
public class ConcreteItemXMap : SubclassMap<ConcreteItemX>
{
public ConcretItemXMap()
{
this.Join("ConcreteItemX", x =>
{
x.KeyColumn("ItemID");
x.Map("FieldC")
});
}
}
希望这可以帮助其他人寻找流利的这种类型的映射.
Hopefully this helps somebody else looking for this type of mapping with fluent.
这篇关于使用Fluent NHibernate进行继承映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!