NHibernate的许多一对多映射 [英] NHibernate many-to-many mapping

查看:159
本文介绍了NHibernate的许多一对多映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不少一对多映射使用NHibernate的问题。基本上我有我的数据库(场景,技能和ScenarioSkill)2班在我的对象模型(方案和技巧)的映射到三个表。该ScenarioSkills表只是拥有的技能和场景表(SkillID,ScenarioID)的ID。

I am having an issue with many-to-many mapping using NHibernate. Basically I have 2 classes in my object model (Scenario and Skill) mapping to three tables in my database (Scenario, Skill and ScenarioSkill). The ScenarioSkills table just holds the IDs of the SKill and Scenario table (SkillID, ScenarioID).

在对象模型场景有几个一般属性和列表关联的技能(IList的),其从ScenarioSkills表获得。有是场景的技能对象没有关联的IList

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. There is no associated IList of Scenarios for the Skill object.

这情景和技能ScenarioSkill的映射是一个多一对多的关系:

The mapping from Scenario and Skill to ScenarioSkill is a many-to-many relationship:

场景--- * * * 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:

在情景类

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

和技能类中。

<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>



一切工作正常,除非我尝试删除一个技能,就不能这样做是有在ScenarioSkill表的SkillID列引用约束。谁能帮我?

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. Can anyone help me?

我使用NHibernate 2上的C#asp.net 3.5 Web应用程序的解决方案。

I am using NHibernate 2 on an C# asp.net 3.5 web application solution.

推荐答案

有点晚就在这里的最后答复,但这个我最终的映射成功实施。

Bit late on the final reply here but this the the mapping I ended up successfully implementing.

在场景

<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>

在技能

    <!-- 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>

这篇关于NHibernate的许多一对多映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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