有谁知道我将如何映射一个具有两个相同子类型的多对多集合的实体。
我的数据库结构是这样的......
“正常”的关系将是......
tbl_Parent
col_Parent_ID
tbl_Parent_Child_Xref
col_Parent_ID
col_Child_ID
tbl_Child
col_Child_ID
另一种关系是...
tbl_Parent
col_Parent_ID
tbl_Include_ParentChild_Xref
col_Parent_ID
col_Child_ID
tbl_Child
col_Child_ID
实体和映射看起来像这样......
public partial class ParentEntity : AuditableDataEntity<ParentEntity>
{
public virtual IList<ChildEntity> Children { get; set; }
public virtual IList<ChildEntity> IncludedChildren { get; set; }
}
public partial class ParentMap : IAutoMappingOverride<ParentEntity>
{
public void Override(AutoMapping<ParentEntity> mapping)
{
mapping.Table("tbl_Parent");
mapping.HasManyToMany(x => x.Children)
.Table("tbl_Parent_Child_Xref")
.ParentKeyColumn("col_Parent_ID")
.ChildKeyColumn("col_Child_ID")
.Inverse()
.Cascade.All();
mapping.HasManyToMany(x => x.IncludedChildren)
.Table("tbl_Include_ParentChild_Xref")
.ParentKeyColumn("col_Parent_ID")
.ChildKeyColumn("col_Child_ID")
.Inverse()
.Cascade.All();
}
}
我得到的错误是
“System.NotSupportedException:无法弄清楚多对多属性'Children'的另一面应该是什么。”
我正在使用 NHibernate 2.1.2,FluentNhibernate 1.0。
最佳答案
FNH 似乎很困惑,因为如果我没记错的话,您似乎将同一个对象(ChildEntity)映射到两个不同的表。
如果您真的不需要将两个列表分开,也许为每个列表使用一个区分值可以解决问题。例如,您的第一个 ChildEntity 列表将绑定(bind)到区分值 A,并且您将绑定(bind)到区分值 B。
否则,我可能会选择您的 ChildEntity 的派生类,只是不要与 ChildEntity 具有相同的名称。
IList<ChildEntity> ChildEntities
IList<IncludedChildEntity> IncludedChildEntities
并且您的两个对象类都是相同的。
如果你说它适用于 NH,那么它可能是一个已经说过的错误。但是,您可以将 XML 映射和 AutoMapping 与 FNH 混合使用。所以,如果它在新罕布什尔州有效,这也许是我的偏好。但是认为这种解决方法应该可以做到。
关于具有多个相同类型的多对多列表的休眠实体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1915947/