我有一个在概念上类似于此的类层次结构:
也就是说,有一个抽象基类 ( Relation
) 和几个派生类。在实践中,Customer
共享Supplier
了很多代码,所以我将共同点重构为一个抽象类BusinessContact
。现在实际的类层次结构如下所示:
或在代码中:
public abstract class Relation
{
public virtual int Id { get; set; }
}
public class ContactPerson : Relation
{
public virtual string PhoneNumber { get; set; }
}
public abstract class BusinessContact : Relation
{
public virtual string Name { get; set; }
}
public class Customer : BusinessContact
{
public virtual string CustomerNumber { get; set; }
}
public class Supplier : BusinessContact
{
public virtual string SupplierNumber { get; set; }
}
我想使用 NHibernate 中的连接子类,使用按代码映射 ( )将此层次结构映射到四个表 ( Relation
、ContactPerson
和)。我的映射如下所示:Customer
Supplier
ModelMapper
var mapper = new ModelMapper();
mapper.Class<Relation>(map =>
{
map.Id(x => x.Id, id => id.Generator(Generators.Native));
});
mapper.JoinedSubclass<ContactPerson>(map =>
{
map.Key(key => key.Column("Id"));
map.Property(x => x.PhoneNumber);
});
mapper.JoinedSubclass<Customer>(map =>
{
map.Key(key => key.Column("Id"));
map.Property(x => x.Name);
map.Property(x => x.CustomerNumber);
});
mapper.JoinedSubclass<Supplier>(map =>
{
map.Key(key => key.Column("Id"));
map.Property(x => x.Name);
map.Property(x => x.SupplierNumber);
});
但是,一旦我尝试将映射添加到配置中,就会出现异常:
NHibernate.MappingException: Cannot extend unmapped class: BusinessContact
我基本上明白为什么会发生这种情况。生成的映射如下所示:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" namespace="JoinedSubClassMapping" assembly="JoinedSubClassMapping" xmlns="urn:nhib ernate-mapping-2.2">
<class name="Relation" abstract="true">
<id name="Id" type="Int32">
<generator class="native" />
</id>
</class>
<joined-subclass name="ContactPerson" extends="Relation">
<key column="Id" />
<property name="PhoneNumber" />
</joined-subclass>
<joined-subclass name="Customer" extends="BusinessContact">
<key column="Id" />
<property name="CustomerNumber" />
</joined-subclass>
<joined-subclass name="Supplier" extends="BusinessContact">
<key column="Id" />
<property name="SupplierNumber" />
</joined-subclass>
</hibernate-mapping>
Customer
并在他们的属性中Supplier
定义,就好像模型中的“正常”实体一样。由于没有 的映射,因此失败,或者当然。请注意,“名称”属性(定义在其中也不会出现在映射中。BusinessContact
extends
BusinessContact
BusinessContact
BusinessContact
我希望映射看起来像这样:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" namespace="JoinedSubClassMapping" assembly="JoinedSubClassMapping" xmlns="urn:nhibernate-mapping-2.2">
<class name="Relation" abstract="true">
<id name="Id" type="Int32">
<generator class="native" />
</id>
</class>
<joined-subclass name="ContactPerson" extends="Relation">
<key column="Id" />
<property name="PhoneNumber" />
</joined-subclass>
<joined-subclass name="Customer" extends="Relation">
<key column="Id" />
<property name="CustomerNumber" />
<property name="Name" />
</joined-subclass>
<joined-subclass name="Supplier" extends="Relation">
<key column="Id" />
<property name="SupplierNumber" />
<property name="Name" />
</joined-subclass>
</hibernate-mapping>
也就是说,创建Supplier
和Customer
扩展 Relation 并包含(否则未映射的)BusinessContact
类的所有映射属性。
我怎样才能做到这一点?