具有多个相同类型的多对多列表的 Nhibernate 实体?

发布于 2024-08-15 12:32:21 字数 1454 浏览 3 评论 0原文

有谁知道我如何将一个实体与两个相同子类型的多对多集合进行映射。

我的数据库结构是这样的...

“正常”关系是...

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。

Does anybody know how I would map an entity with two many-to-many collections of the same child type.

My database structure is this....

The "normal" relationship will be....

tbl_Parent
  col_Parent_ID

tbl_Parent_Child_Xref
   col_Parent_ID
   col_Child_ID

tbl_Child
   col_Child_ID

The alternative relationship is...

tbl_Parent
  col_Parent_ID

tbl_Include_ParentChild_Xref
   col_Parent_ID
   col_Child_ID

tbl_Child
   col_Child_ID

The entity and mapping look like this...

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();
    }
}

The error that I'm getting is
"System.NotSupportedException: Can't figure out what the other side of the many-to-many property 'Children' should be."

I'm using NHibernate 2.1.2, FluentNhibernate 1.0.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷清清 2024-08-22 12:32:21

看来 FNH 很困惑,因为如果我没有记错的话,您似乎将同一个对象(ChildEntity)映射到两个不同的表。

如果您确实不需要将两个列表分开,也许为每个列表使用区分值可以解决问题。例如,您的第一个 ChildEntity 列表将绑定到判别值 A,然后您将绑定到判别值 B。

否则,我可能会选择您的 ChildEntity 的派生类,只是不要与 ChildEntity 具有相同的名称。

IList<ChildEntity> ChildEntities
IList<IncludedChildEntity> IncludedChildEntities

并且您的两个对象类将是相同的。

如果你说它适用于 NH,那么它可能是一个错误,正如已经指出的那样。但是,您可以将 XML 映射和 AutoMapping 与 FNH 混合使用。所以,如果它确实在新罕布什尔州有效,这可能是我的偏好。但认为这个解决方法应该可以做到。

It seems FNH is confused because you seem to map the same object (ChildEntity) to two different tables, if I'm not mistaken.

If you don't really need the two lists to get separated, perhaps using a discriminating value for each of your lists would solve the problem. Your first ChildEntity list would bind to the discriminationg value A, and you sesond to the discriminating value B, for instance.

Otherwise, I would perhaps opt for a derived class of your ChildEntity, just not to have the same name of ChildEntity.

IList<ChildEntity> ChildEntities
IList<IncludedChildEntity> IncludedChildEntities

And both your objects classes would be identitical.

If you say it works with NH, then it might be a bug as already stated. However, you may mix both XML mappings and AutoMapping with FNH. So, if it does work in NH, this would perhaps be my preference. But think this workaround should do it.

美羊羊 2024-08-22 12:32:21

您知道我只是在黑暗中拍摄,但听起来 Hibernate 不知道您的 ChildEntity 类..这通常是我看到此类消息的地方。 Hibernate 检查您的类并查看 id 不知道的引用类(在本例中为 ChildEntity)。

也许您已经继续前进并发现了问题,但您认为无论如何我都会看到。

You know I'm just shooting in the dark here, but it almost sounds like your ChildEntity class isn't known by Hibernate .. that's typically where I've seen that sort of message. Hibernate inspects your class and sees this referenced class (ChildEntity in this case) that id doesn't know about.

Maybe you've moved on and found the issue at this point, but thought I'd see anyway.

暮倦 2024-08-22 12:32:21

Fluent 会感到困惑,因为您两次引用同一父列。这是一个禁忌。据我所看到的活动来看,修复不会很快出现。

如果可能的话,您必须编写一些自定义扩展才能使其正常工作。

Fluent is confused because you are referencing the same parent column twice. That is a no-no. And as far as I can tell from the activity i have seen, a fix is not coming any time soon.

You would have to write some custom extensions to get that working, if it is possible.

眼波传意 2024-08-22 12:32:21

非常遗憾的是,NHibernate 无法做到这一点。考虑使用另一个 ORM。

To my great pity, NHibernate cannot do that. Consider using another ORM.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文