fluent nhibernate - 同一实体上的多对多关系映射 [英] fluent nhibernate - many-to-many relationship mapping on same entity

查看:28
本文介绍了fluent nhibernate - 同一实体上的多对多关系映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试绘制多对多关系时遇到问题,其中关系的双方都引用同一个实体.我正在使用 Fluent NHibernate 和 NH3.1.

I am having a problem trying to map out a many-to-many relationship , where both sides of the relationship reference the same entity. I am using Fluent NHibernate and NH3.1.

基本上,场景是这样的 - 我有一个类别,它可以有多个父项.因此,一个类别有多个其他类别作为父类别,以及多个其他类别作为其子类别.

Basically, the scenario is this - I have a category, which can have multiple parents. Thus, a category has multiple other categories as parents, as well as multiple other categories as its children.

HasManyToMany(x => x.ParentCategories).AsBag().Table("parentcategorychildren").ParentKeyColumn("ChildID").ChildKeyColumn("ParentID").Cascade.SaveUpdate();
HasManyToMany(x => x.ChildrenCategories).AsBag().Table("parentcategorychildren").ParentKeyColumn("ParentID").ChildKeyColumn("ChildID").Inverse();

但是,当我尝试构建工厂时,出现以下错误:

However, when I try to build the factory, I get the following error:

Category.ChildrenCategories 与 Category.ChildrenCategories 的关系在双方都指定了 Inverse.从关系的一侧删除 Inverse.

The relationship Category.ChildrenCategories to Category.ChildrenCategories has Inverse specified on both sides. Remove Inverse from one side of the relationship.

我发现奇怪的是为什么它提到Category.ChildrenCategories"到Category.ChildrenCategories,而不是ParentCategories?

What I am finding strange is why is it mentioning 'Category.ChildrenCategories' to Category.ChildrenCategories, as opposed to ParentCategories?

任何帮助将不胜感激!

我刚刚为此创建了一个赏金,因为它对我来说足够重要.拜托,我对回答你不能这样做"不感兴趣.

推荐答案

这很可能是一个 FNH 错误,并且很可能已经在最新的 FNH 源代码.使用FNH1.0和NH2.1时没有问题.等效的 HBM 映射在 FNH1.2 和 NH3.1 中运行良好:

This is most likely a FNH bug and it is most likely already fixed in the latest FNH source code. There is no problem when using FNH1.0 and NH2.1. Equivalent HBM mapping works well in FNH1.2 and NH3.1:

<bag name="ParentCategories" cascade="all" table="parentcategorychildren">
    <key column="ChildID" />
    <many-to-many column="ParentID" class="Category" />
</bag>

<bag name="ChildrenCategories" inverse="true" table="parentcategorychildren">
    <key column="ParentID" />
    <many-to-many column="ChildID" class="Category" />
</bag>

在挖掘 FNH 源代码后,我找到了一个解决方法.假设您的配置如下所示:

After digging in FNH source code I found a workaround. Let's say, your configuration looks like this:

.Mappings(m => {
    m.FluentMappings.AddFromAssemblyOf<Category>();
})

这个配置可以抑制倒霉的代码:

The unlucky code can be suppressed by this configuration:

.Mappings(m => {
    var persistenceModel = new PersistenceModel();
    persistenceModel.AddMappingsFromAssembly(typeof(Category).Assembly);
    persistenceModel.ValidationEnabled = false; // this makes the trick
    m.UsePersistenceModel(persistenceModel);
})

这篇关于fluent nhibernate - 同一实体上的多对多关系映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆