我有一个类用于记录系统中所有其他类的更改:
public class HistoryLogEntry
{
public virtual long Id { get; set; }
public virtual string EntityQualifiedId { get; set; } // <-- this contains both the entity name and entity id to properly identify object
public virtual long? IdEntity { get; set; }
public virtual DateTime? EntryDate { get; set; }
public virtual UserInfo CreatedBy { get; set; }
public virtual string LogData { get; set; }
}
我想要其他具有 HistoryLogEntry 对象列表的对象:
public class Person
{
[...]
public virtual IList<HistoryLogEntry> HistoryLogEntryList { get; set; }
}
public class Dog
{
[...]
public virtual IList<HistoryLogEntry> HistoryLogEntryList { get; set; }
}
请注意,HistoryLogEntry 没有对 Person 或 Dog 的引用——这没关系,因为我想在 Person 和 Dog 中都使用 HistoryLogEntry 类。
我如何映射这种关系,以便 NHibernate 的导出模式将创建所有必要的列?现在我正在尝试:
public class HistoryLogEntryMapping : ClassMapping<HistoryLogEntry>
{
public HistoryLogEntryMapping()
{
Lazy( true );
Table( MappingSettings.GetTableName( "HistoryLogEntry", "SYS" ) );
Id( x => x.Id, map => map.Generator( new ISLib3.Storage.NH.MultipleHiLoPerTableGeneratorDef() ) );
Property( x => x.EntityQualifiedId, map => { map.Length( 255 ); map.NotNullable( true ); map.Index( "HistoryLogEntry_EntityQualifiedId" ); } );
Property( x => x.IdEntity, map => { map.NotNullable( false ); } );
Property( x => x.EntryDate );
ManyToOne( x => x.CreatedBy, map =>
{
map.Cascade( Cascade.None );
map.Column( "IdCreatedBy" );
map.Lazy( LazyRelation.Proxy );
map.NotNullable( false );
map.NotFound( NotFoundMode.Ignore );
} );
Property( x => x.LogData, map => { map.NotNullable( false ); map.Type( NHibernateUtil.StringClob ); } );
}
}
public class PersonMapping : ClassMapping<Person>
{
public PersonMapping()
{
Lazy( true );
BatchSize( 10 );
Table( MappingSettings.GetTableName( "Person" ) );
Id( x => x.Id, map => map.Generator( new ISLib3.Storage.NH.MultipleHiLoPerTableGeneratorDef() ) );
Bag( x => x.HistoryLogEntryList, map =>
{
// foreign key name MUST be set to "none" - this way ExportSchema will not generate keys in DB!
map.Key( k => { k.Column("IdEntity" ); k.ForeignKey( "none" ); k.OnDelete( OnDeleteAction.NoAction ); } );
map.Cascade( Cascade.None );
map.Loader( "historyLogLoader" ); // <-- note that I have special loader to load HistoryLogEntry based on column EntityQualifiedId
map.Lazy( CollectionLazy.Lazy );
map.Inverse( true );
map.BatchSize( 10 );
}, rm=>rm.OneToMany() );
}
}
但是,当使用 NHibernate 3.3 和 ExportSchemat 时,它不会在 HistoryLogEntry 中创建 IdEntity 列,我相信我需要这个列,以便 NHibernate 知道 HistoryLogEntries 放入我的集合中。
任何帮助将不胜感激。