我想在nhibernate thru代码中设置table-per-classhierarchy继承。除子类外,其他所有内容都在XML映射文件中设置。如果我在xml中的子类都很好,但不是来自代码。这是我使用的代码 - 我的具体子类永远不会被创建:(
//the call
NHibernate.Cfg.Configuration config = new NHibernate.Cfg.Configuration();
SetSubclass(config, typeof(TAction), typeof(tActionSub1), "Procedure");
//the method
public static void SetSubclass(Configuration configuration, Type baseClass, Type subClass, string discriminatorValue)
{
PersistentClass persBaseClass = configuration.ClassMappings.Where(cm => cm.MappedClass == baseClass).Single();
SingleTableSubclass persSubClass = new SingleTableSubclass(persBaseClass);
persSubClass.ClassName = subClass.AssemblyQualifiedName;
persSubClass.DiscriminatorValue = discriminatorValue;
persSubClass.EntityPersisterClass = typeof(SingleTableEntityPersister);
persSubClass.ProxyInterfaceName = (subClass).AssemblyQualifiedName;
persSubClass.NodeName = subClass.Name;
persSubClass.EntityName = subClass.FullName;
persBaseClass.AddSubclass(persSubClass);
}
Xml映射如下所示:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Riz.Pcm.Domain.BusinessObjects" assembly="Riz.Pcm.Domain">
<class name="Riz.Pcm.Domain.BusinessObjects.TAction, Riz.Pcm.Domain" table="dbo.tAction" lazy="true">
<id name="Id" column="ID">
<generator class="guid" />
</id>
<discriminator type="String" formula="(select jt.Name from TJobType jt where jt.Id=JobTypeId)" insert="true" force="false"/>
<many-to-one name="Session" column="SessionID" class="TSession" />
<property name="Order" column="Order1" />
<property name="ProcessStart" column="ProcessStart" />
<property name="ProcessEnd" column="ProcessEnd" />
<property name="Status" column="Status" />
<many-to-one name="JobType" column="JobTypeID" class="TJobType" />
<many-to-one name="Unit" column="UnitID" class="TUnit" />
<bag name="TActionProperties" lazy="true" cascade="all-delete-orphan" inverse="true" >
<key column="ActionID"></key>
<one-to-many class="TActionProperty"></one-to-many>
</bag>
<!--<subclass name="Riz.Pcm.Domain.tActionSub" discriminator-value="ZPower"></subclass>-->
</class>
</hibernate-mapping>
我做错了什么?我在谷歌上找不到任何例子:(
答案 0 :(得分:0)
我们正在使用FluentNHibernate进行映射。 User实体是Learner,Assessor,Admin等的基类。
以下是一个例子:
public class UserMap : ClassMap<User>
{
public UserMap()
{
this.Id(x => x.Id);
this.Map(x => x.Active);
this.Component(
x => x.Address,
m =>
{
m.Map(x => x.Address1).Length(512);
m.Map(x => x.Address2);
m.Map(x => x.Address3);
m.Map(x => x.Address4);
m.Map(x => x.City);
m.Map(x => x.County);
m.Map(x => x.PostCode);
m.References(x => x.Country);
});
this.References(x => x.CreatedBy);
this.Map(x => x.CreatedDate).Not.Nullable();
this.DiscriminateSubClassesOnColumn("className").Length(64);
}
}
和派生类
public class LearnerMap : SubclassMap<Learner>
{
#region Constructors and Destructors
/// <summary>
/// Initializes a new instance of the <see cref="LearnerMap"/> class.
/// </summary>
public LearnerMap()
{
this.Map(x => x.DateOfBirth);
this.Map(x => x.NINumber);
this.Map(x => x.IscCodeId);
this.Map(x => x.ScnNo);
this.Map(x => x.ULN);
this.HasManyToMany(x => x.Organisation)
.ParentKeyColumn("userId")
.ChildKeyColumn("organisationId").Table(
"UserOrganisations");
}
希望这会有所帮助