简体   繁体   English

NHibernate多对多映射

[英]NHibernate many-to-many mapping

I am having an issue with many-to-many mapping using NHibernate. 我在使用NHibernate进行多对多映射时遇到问题。 Basically I have 2 classes in my object model (Scenario and Skill) mapping to three tables in my database (Scenario, Skill and ScenarioSkill). 基本上,我的对象模型(场景和技能)中有2个类映射到数据库中的三个表(场景,技能和ScenarioSkill)。 The ScenarioSkills table just holds the IDs of the SKill and Scenario table (SkillID, ScenarioID). ScenarioSkills表仅包含SKill和Scenario表的ID(SkillID,ScenarioID)。

In the object model a Scenario has a couple of general properties and a list of associated skills (IList) that is obtained from the ScenarioSkills table. 在对象模型中,场景具有几个常规属性以及从ScenarioSkills表中获得的关联技能列表(IList)。 There is no associated IList of Scenarios for the Skill object. 此技能对象没有关联的场景清单。

The mapping from Scenario and Skill to ScenarioSkill is a many-to-many relationship: 从Scenario和Skill到ScenarioSkill的映射是多对多关系:

Scenario * --- * ScenarioSkill * --- * Skill 场景* --- *场景技能* --- *技能

I have mapped out the lists as bags as I believe this is the best option to use from what I have read. 我已经将这些列表作为购物袋进行了映射,因为我认为这是我所阅读的书中最好的选择。 The mappings are as follows: 映射如下:

Within the Scenario class 在场景类中

<bag name="Skills" table="ScenarioSkills">
  <key column="ScenarioID" foreign-key="FK_ScenarioSkill_ScenarioID"/>
  <many-to-many class="Domain.Skill, Domain" column="SkillID" />
</bag>

And within the Skill class 在技​​能课中

<bag name="Scenarios" table="ScenarioSkills" inverse="true" access="noop" cascade="all">
  <key column="SkillID" foreign-key="FK_ScenarioSkill_SkillID" />
  <many-to-many class="Domain.Scenario, Domain" column="ScenarioID" />
</bag>

Everything works fine, except when I try to delete a skill, it cannot do so as there is a reference constraint on the SkillID column of the ScenarioSkill table. 一切工作正常,除非我尝试删除一项技能,否则无法做到这一点,因为ScenarioSkill表的SkillID列上存在引用约束。 Can anyone help me? 谁能帮我?

I am using NHibernate 2 on an C# asp.net 3.5 web application solution. 我在C#asp.net 3.5 Web应用程序解决方案上使用NHibernate 2。

Unless I'm reading the question incorrectly you need to delete the related ScenarioSkills before you delete a Skill or a Scenario. 除非我没有正确阅读问题,否则您需要先删除相关的ScenarioSkills,然后再删除一项技能或一种方案。 It's pretty straighforward, you just need a custom method to delete the relates ScenarioSkill objects before you delete the parent record. 这很简单,在删除父记录之前,您只需要一个自定义方法即可删除相关ScenarioSkill对象。

Do you want it to delete automatically though? 您是否要自动删除?

You'll want to set cascade="all-delete-orphan" on Skills many-to-many linking to ScenarioSkills. 您需要在技能多对多链接到ScenarioSkills的技能上设置cascade="all-delete-orphan" Just like it sounds, it will delete the orphaned records and keep that error from popping up. 就像听起来一样,它将删除孤立的记录并防止弹出该错误。

As a side note, many-to-many's should be used with care. 附带说明,应谨慎使用多对多。 Most many-to-manys contain other information in the relationship and are better mapped as a set of one-to-manys. 大多数多对多关系中包含其他信息,最好将其映射为一组多对多关系。

Bit late on the final reply here but this the the mapping I ended up successfully implementing. 最后的答复在这里有点晚,但这是我最终成功实现的映射。

In Scenario 在场景中

<bag name="skills" access="field" schema="OSM" table="ScenarioSkill" cascade="none">
  <key column="ScenarioID"
       foreign-key="FK_ScenarioSkill_Scenario" />

  <!-- Skills can be soft-deleted (groan), so ignore them if they don't 'exist' anymore. -->
  <many-to-many column="SkillID"
                class="DomainModel.Skill, DomainModel"
                foreign-key="FK_ScenarioSkill_Skill"
                where="IsDeleted = 0"/>
</bag>

In Skill 在技​​巧上

    <!-- inverse end of scenarios -->
<bag name="scenarios" access="field" inverse="true" schema="OSM" table="ScenarioSkill" cascade="none">
  <key column="SkillID"
       foreign-key="FK_ScenarioSkill_Skill" />
  <many-to-many column="ScenarioID"
                class="Scenario"
                foreign-key="FK_ScenarioSkill_Scenario" />
</bag>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM