我有一个类不能映射到一个显式表。它映射到的表取决于使用它的上下文。基本上它的工作方式应该与我试图将 an 绑定IDictionary<string, int>
到表一样。
// this is a mapped entity, with a Guid Id, but it can't be mapped to one explicit table
public class NoTableClass : Entity<Guid>
{
}
// the table that this class gets its MyCollectionContext1 from depends on the HasMany mapping for that property
public class MappedClass1 : Entity<Guid>
{
public IEnumerable<NoTableClass> MyCollectionContext1 { get; set; }
}
// the table that this class gets its MyCollectionContext2 from depends on the HasMany mapping for that property
public class MappedClass2 : Entity<Guid>
{
public IEnumerable<NoTableClass> MyCollectionContext2 { get; set; }
}
// Mapping overrides
public void Override(AutoMapping<MappedClass1> mapping)
{
mapping.HasMany(Reveal.Member<MappedClass1, IEnumerable<NoTableClass>>("MyCollectionContext1"))
.Table("Table1")
.Access.Field()
.Cascade.AllDeleteOrphan();
}
public void Override(AutoMapping<MappedClass2> mapping)
{
mapping.HasMany(Reveal.Member<MappedClass2, IEnumerable<NoTableClass>>("MyCollectionContext2"))
.Table("Table2")
.Access.Field()
.Cascade.AllDeleteOrphan();
}
.Table()
当集合具有自定义类时,NHibernate 似乎忽略了映射方法。我不明白为什么可以以这种方式映射简单类型的字典,但具有自定义类的集合却不能。
注意:我有我的理由不想使用继承/泛型来解决这个问题,并且不想为我映射到的每个表都有一个具体的类型。我们有一个使用泛型的可行解决方案,但它会给我们带来其他问题。