Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to delete a child from a bidirectional many-to-many association?
    primarykey
    data
    text
    <p>How can I delete a child from a bidirectional <strong>many-to-many association</strong>?</p> <p>Deleting the child does not work because I get an exception (FK Violation).</p> <p>Just removing the child from the parent and call saveorupdate on the parent does not do anything.</p> <p>Entities:</p> <pre><code>public class Employee : Entity { public virtual string LastName { get; set; } public virtual string FirstName { get; set; } public virtual string EMail { get; set; } public virtual IList&lt;LoanedItem&gt; LoanedItems { get; set; } public virtual ISet&lt;Team&gt; Teams { get; set; } public Employee() { if (LoanedItems == null) { LoanedItems = new List&lt;LoanedItem&gt;(); } if (Teams == null) { Teams = new HashedSet&lt;Team&gt;(); } } public virtual void AddTeam(Team team) { Teams.Add(team); team.Employees.Add(this); } public virtual void RemoveTeamFromEmployee(Team team) { Teams.Remove(team); } } public class Team : Entity { public virtual string Name { get; set; } public virtual ISet&lt;Employee&gt; Employees { get; set; } public Team() { if (Employees == null) { Employees = new HashedSet&lt;Employee&gt;(); } } public virtual void RemoveEmployeeFromTeam (Employee employee) { var result = Employees.Remove(employee); } public virtual void AddEmployee(Employee employee) { Employees.Add(employee); employee.Teams.Add(this); } } </code></pre> <p>Mappings:</p> <pre><code>public class TeamMap : ClassMap&lt;Team&gt; { public TeamMap() { // identity mapping Id(p =&gt; p.Id) .Column("TeamId") .GeneratedBy.Identity(); // column mapping Map(p =&gt; p.Name); // Employee is responible for the relationship HasManyToMany(p =&gt; p.Employees) .Table("TeamEmployee") .AsSet() .LazyLoad() .Inverse() .Cascade.SaveUpdate() .ParentKeyColumn("TeamId") .ChildKeyColumn("EmployeeId") .NotFound.Ignore(); } } public class EmployeeMap : ClassMap&lt;Employee&gt; { public EmployeeMap() { // identifier mapping Id(p =&gt; p.Id) .Column("EmployeeId") .GeneratedBy.Identity(); // column mapping Map(p =&gt; p.EMail); Map(p =&gt; p.LastName); Map(p =&gt; p.FirstName); // Employee is responible for the relationship HasManyToMany(p =&gt; p.Teams) .Table("TeamEmployee") .AsSet() .LazyLoad() .ParentKeyColumn("EmployeeId") .ChildKeyColumn("TeamId") .Cascade.SaveUpdate() .NotFound.Ignore(); HasMany(p =&gt; p.LoanedItems) .Cascade.SaveUpdate() .KeyColumn("EmployeeId"); } } </code></pre> <p><strong>EDIT</strong></p> <p>Code to delete a team:</p> <pre><code>//Test Case 2 //Delete Team 2 Team team = session.Get&lt;Team&gt;(2); List&lt;Employee&gt; employees = team.Employees.ToList(); foreach (var employee in employees) { team.RemoveEmployeeFromTeam(employee); } session.Delete(team); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload