NHibernate many-to-many mapping
Posted
by Scozzard
on Stack Overflow
See other posts from Stack Overflow
or by Scozzard
Published on 2009-07-05T23:27:50Z
Indexed on
2010/03/08
5:06 UTC
Read the original article
Hit count: 238
Hi,
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).
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.
The mapping from Scenario and Skill to ScenarioSkill is a many-to-many relationship:
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. Can anyone help me?
I am using NHibernate 2 on an C# asp.net 3.5 web application solution.
© Stack Overflow or respective owner