Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 520263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:07:37+00:00 2026-05-13T08:07:37+00:00

How can I delete a child from a bidirectional many-to-many association ? Deleting the

  • 0

How can I delete a child from a bidirectional many-to-many association?

Deleting the child does not work because I get an exception (FK Violation).

Just removing the child from the parent and call saveorupdate on the parent does not do anything.

Entities:

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<LoanedItem> LoanedItems { get; set; }
    public virtual ISet<Team> Teams { get; set; }

    public Employee()
    {
        if (LoanedItems == null)
        {
            LoanedItems = new List<LoanedItem>();
        }

        if (Teams == null)
        {
            Teams = new HashedSet<Team>();
        }
    }

    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<Employee> Employees { get; set; }

    public Team()
    {
        if (Employees == null)
        {
            Employees = new HashedSet<Employee>();
        }
    }

    public virtual void RemoveEmployeeFromTeam (Employee employee)
    {
        var result = Employees.Remove(employee);
    }

    public virtual void AddEmployee(Employee employee)
    {
        Employees.Add(employee);
        employee.Teams.Add(this);
    }
}

Mappings:

public class TeamMap : ClassMap<Team>
{
    public TeamMap()
    {
        // identity mapping
        Id(p => p.Id)
            .Column("TeamId")
            .GeneratedBy.Identity();

        // column mapping
        Map(p => p.Name);

        // Employee is responible for the relationship
        HasManyToMany(p => p.Employees)
            .Table("TeamEmployee")
            .AsSet()
            .LazyLoad()
            .Inverse()
            .Cascade.SaveUpdate()
            .ParentKeyColumn("TeamId")
            .ChildKeyColumn("EmployeeId")
            .NotFound.Ignore();

    }
}

public class EmployeeMap : ClassMap<Employee>
{
    public EmployeeMap()
    {
        // identifier mapping
        Id(p => p.Id)
            .Column("EmployeeId")
            .GeneratedBy.Identity();

        // column mapping
        Map(p => p.EMail);
        Map(p => p.LastName);
        Map(p => p.FirstName);

        // Employee is responible for the relationship
        HasManyToMany(p => p.Teams)
            .Table("TeamEmployee")
            .AsSet()
            .LazyLoad()
            .ParentKeyColumn("EmployeeId")
            .ChildKeyColumn("TeamId")
            .Cascade.SaveUpdate()
            .NotFound.Ignore();

        HasMany(p => p.LoanedItems)
            .Cascade.SaveUpdate()
            .KeyColumn("EmployeeId");
    }
}

EDIT

Code to delete a team:

//Test Case 2
//Delete Team 2
Team team = session.Get<Team>(2);

List<Employee> employees = team.Employees.ToList();

foreach (var employee in employees)
{
    team.RemoveEmployeeFromTeam(employee);
}

session.Delete(team);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T08:07:38+00:00Added an answer on May 13, 2026 at 8:07 am

    If it is a many-to-many, then one direction is the “master” direction. The other direction will have inverse="false" defined. You have to remove from the collection the right way round – to be sure (and to truly represent what your deletion is achieving), delete both. Then flush the session to have this persisted.

    Example:

    if A has a collection of objects of type B, and vice versa, then to remove the association between two particular instances, you have to delete the instance of A from the instance of B‘s collection and vice versa. Then when the session is flushed, NHibernate knows to delete the relevant row from the linking table.

    Edit now you’ve posted code

    Try changing the two methods like so:

    public virtual void RemoveTeamFromEmployee(Team team)
    {
        Teams.Remove(team);
        team.Employees.Remove(this);
    }
    
    public virtual void RemoveEmployeeFromTeam (Employee employee)
    {
        Employees.Remove(employee);
        employee.Teams.Remove(this);
    }
    

    To tidy up your code to delete a team, which is not responsible for the relationship, you do have to iterate over the linked employees. However, if you have to delete teams more frequently than employees, then you may find it more convenient to reverse this, and then you can just use:

    team.Employees.Clear();
    session.Delete(team);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I trying to figure out how I can delete from multiple tables in SQL
From the documentation: If the platform supports the unsetenv() function, you can delete items
How can I delete the hidden cropped elements from a pdf from command line?
How can i delete a html-special char \ from a string. $string = 'This
how can I delete IE 8 cookies for a certain site from Python?
I have a plsql procedure to delete records from child and parent tables. I
How can we use the cascade in PostgreSQL while deleting the one record from
I want to delete certain records from a table. These records have some child-records
I am trying to delete the child element in the dom from its parent
How I can delete an object which I had added before with this code.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.