大家好,
我正在努力解决一个 Fluent NHibernate 问题。我的解决方案中有以下类结构模板:
class OneClass
{
public virtual string OneProp {get; set;}
}
class TwoClass : OneClass
{
public virtual string TwoProp {get; set;}
}
class ThreeClass : TwoClass
{
public virtual string ThreeProp {get; set;}
}
而且我想对我的类使用每个继承表的层次结构策略,以包含数据库中一个表中的所有数据。
如何通过 Fluent NHibernate 做到这一点?
我尝试过以下案例:
1.我为父类添加了判别器
public class OneClassMappingOverride : IAutoMappingOverride<OneClass>
{
public void Override(AutoMapping<OneClass> mapping)
{
mapping.DiscriminateSubClassesOnColumn("Type");
mapping.SubClass<OneClass>("OneClass");
mapping.SubClass<TwoClass>("TwoClass");
mapping.SubClass<ThreeClass>("ThreeClass");
}
}
但我有例外:(XmlDocument)(56,8):XML验证错误:命名空间'urn:nhibernate-mapping-2.2'中的元素'subclass'在命名空间'urn中有无效的子元素'joined-subclass': nhibernate-mapping-2.2'。预期的可能元素列表:'meta、tuplizer、同步、属性、多对一、一对一、组件、动态组件、属性、任何、映射、集合、列表、包、idbag、数组、原语-array,join,subclass,loader,sql-insert,sql-update,sql-delete,resultset,query,sql-query'在命名空间'urn:nhibernate-mapping-2.2'中。
因为我有以下为 NHibernate 自动生成的 xml 映射:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class xmlns="urn:nhibernate-mapping-2.2" name="MyClass.Domain.OneClass, MyClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`OneClass`">
<cache usage="read-write" />
<id name="PersistenceId" type="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="Systematic.Persistence.NHibernate.NHibernateIdGenerator, Systematic.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
</id>
<discriminator type="String">
<column name="Type" />
</discriminator>
<version generated="never" name="PersistedVersion" type="System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0">
<column name="PersistedVersion" />
</version>
<property name="OneProp" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="OneProp" length="255" />
</property>
<property name="DisplayName" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="DisplayName" length="256" index="idx__DisplayName" />
</property>
<property name="SystemName" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="SystemName" length="256" index="idx__SystemName" not-null="true" />
</property>
<property name="Version" type="System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Version" not-null="true" />
</property>
<property name="Description" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Description" length="10000000" />
</property>
<many-to-one class="Systematic.Persistence.PersistenceInfo, Systematic.Api, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" fetch="join" lazy="false" name="Persistence">
<column name="Persistence_id" index="idx__Persistence" not-null="true" />
</many-to-one>
<subclass name="MyClass.Domain.TwoClass, MyClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<property name="TwoProp" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="TwoProp" length="255" />
</property>
<joined-subclass name="MyClass.Domain.ThreeClass, MyClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<key>
<column name="TwoClass_id" />
</key>
<property name="ThreeProp" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="ThreeProp" length="255" />
</property>
</joined-subclass>
</subclass>
</class>
</hibernate-mapping>
2.我尝试添加不同的鉴别器
public class OneClassMappingOverride : IAutoMappingOverride<OneClass>
{
public void Override(AutoMapping<OneClass> mapping)
{
mapping.DiscriminateSubClassesOnColumn("OneType");
}
}
public class TwoClassMappingOverride : IAutoMappingOverride<TwoClass>
{
public void Override(AutoMapping<TwoClass> mapping)
{
mapping.DiscriminateSubClassesOnColumn("TwoType");
}
}
但系统没有任何结果(相同的异常和映射文件)
3. 我尝试过使用 ClassMap<> 和 SubclassMap<>
public class OneClassMap : ClassMap<OneClass>
{
public OneClassMap()
{
DiscriminateSubClassesOnColumn("Type");
Id(x => x.Id);
Map(x => x.OneProp);
}
}
public class TwoClassMap : SubclassMap<TwoClass>
{
public TwoClassMap()
{
DiscriminatorValue("TwoType");
Map(x => x.TwoProp);
}
}
public class ThreeClassMap : SubclassMap<ThreeClass>
{
public ThreeClassMap()
{
DiscriminatorValue("ThreeType");
Map(x => x.ThreProp);
}
}
在这种情况下,我有三个表(我的数据库中的 OneClass、TwoClass 和 ThreeClass)还有以下 xml 映射文件:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class xmlns="urn:nhibernate-mapping-2.2" name="MyClass.Domain.OneClass, MyClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" table="`OneClass`">
<cache usage="read-write" />
<id name="PersistenceId" type="System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="Systematic.Persistence.NHibernate.NHibernateIdGenerator, Systematic.Core, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
</id>
<version generated="never" name="PersistedVersion" type="System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" unsaved-value="0">
<column name="PersistedVersion" />
</version>
<property name="OneProp" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="OneProp" length="255" />
</property>
<property name="DisplayName" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="DisplayName" length="256" index="idx__DisplayName" />
</property>
<property name="SystemName" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="SystemName" length="256" index="idx__SystemName" not-null="true" />
</property>
<property name="Version" type="System.Int64, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Version" not-null="true" />
</property>
<property name="Description" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Description" length="10000000" />
</property>
<many-to-one class="Systematic.Persistence.PersistenceInfo, Systematic.Api, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" fetch="join" lazy="false" name="Persistence">
<column name="Persistence_id" index="idx__Persistence" not-null="true" />
</many-to-one>
<joined-subclass name="MyClass.Domain.TwoClass, MyClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<key>
<column name="OneClass_id" />
</key>
<property name="TwoProp" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="TwoProp" length="255" />
</property>
<joined-subclass name="MyClass.Domain.ThreeClass, MyClass, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<key>
<column name="TwoClass_id" />
</key>
<property name="ThreeProp" type="System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="ThreeProp" length="255" />
</property>
</joined-subclass>
</joined-subclass>
</class>
</hibernate-mapping>
我不知道如何解决我的问题。也许有人帮我解决这个问题。
非常感谢。
真诚的,阿列克谢