本文介绍了功能NHibernate - 在同一个实体不少一对多映射关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题试图绘制出一个多到多的关系,其中所述关系的两侧引用相同的实体。我使用流利的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已经逆双方指定。从这种关系中的一个侧面卸下逆。

什么我发现奇怪的是为什么会提'Category.ChildrenCategories来Category.ChildrenCategories,而不是ParentCategories?

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

任何帮助将大大AP preciated!

Any help would be greatly appreciated !

我刚刚创建这个赏金,因为这对我来说足够重要。拜托,我不感兴趣,你不能这样做作为回答。

推荐答案

这是最有可能是错误的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源$ C ​​$ C挖后,我找到了一个解决方法。比方说,你的配置是这样的:

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

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

倒霉code可通过此配置pssed SUP $ P $:

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

这篇关于功能NHibernate - 在同一个实体不少一对多映射关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-05 01:24