1
我有一个无法映射到一个显式表的类。它所映射的表取决于使用它的上下文。基本上它应该如同我试图将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();
}
好像忽略了NHibernate的集合时有一个自定义类.Table()
映射方法。我不明白为什么可以用这种方式映射简单类型的字典,但是具有自定义类的集合不能。
注意:我有我的理由不想使用继承/泛型来解决这个问题,并且不想让我映射到每个表的具体类型。我们有一个使用泛型的工作解决方案,但它为我们创造了其他问题。
完美的作品,我一直在寻找这种解决方案为期一天,谢谢你:)有一个问题。我看不出任何方法来映射'NoTableClass'的主键,以便每次插入都会生成一个新的'Guid'。这通常是通过扩展'Entity'或在类映射中通过使用'.Id()'来实现的。我可以在'.Component()'里面做的唯一事情是'c.Map(x => x。 Id)'这将只是插入一个空的Guid。一旦我解决这个问题,我会接受你的答案。 –
如果你所需要的只是'Guid'的生成,那么你可以在'NoTableClass' contrsuctor:'thid.Id = Guid.NewGuid()'中生成它。 – SHSE
我们真的不想在代码中生成guid,我们的整个系统使用Guid作为唯一标识符,并且在插入时由数据库生成,我们不希望在代码中生成id插入之前。 –