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 479507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:47:58+00:00 2026-05-13T00:47:58+00:00

I have a many to many relationship between a Team and an Employee entity.

  • 0

I have a many to many relationship between a Team and an Employee entity.

I mapped them as following:

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

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

        // relationship mapping
        HasManyToMany(m => m.Teams).Table("EmployeeTeam")
            .Inverse()
            .Cascade.All()
            .AsSet()
            .LazyLoad()
            .ParentKeyColumn("EmployeeID")
            .ChildKeyColumn("TeamID");

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

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

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

        // relationship mapping
        HasManyToMany(m => m.Employees)
            .Table("EmployeeTeam")
            .LazyLoad()
            .Cascade.All()
            .AsSet()
            .ParentKeyColumn("TeamID")
            .ChildKeyColumn("EmployeeID");
    }
}

Then I created 3 Teams and 2 Employees:

TeamID  EmployeeID
1       1
1       2
2       2
3       1

The Employee1 has also 2 LoanedItems(Books, Magazines). Employee2 has no LoanedItems.

Now I want to delete Employee1, who is in Team1 and Team3. In Team 1 is also Employee2.
So when I delete Employee1, I assume that Employee1 is deleted and also Team3, because I also assume that an Team can only exist when it has an Employe and vice versa. So Team1 may not be deleted, because it has Employee2 and can still exists.

I used the following code lines with a new Session:

var loadedEmployee = session.Get<Employee>(1);
session.Delete(loadedEmployee);
transaction.Commit();

But what happens?
-> NHibernate deletes all Teams and all Employees!
-> NHibernate updated my LoanedItem table correctly by setting the FK EmployeeID to NULL.

What is wrong there?

  • 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-13T00:47:58+00:00Added an answer on May 13, 2026 at 12:47 am

    I have answered a similar question here:
    What is the correct way to define many-to-many relationships in NHibernate to allow deletes but avoiding duplicate records

    Reading the question and my answer maybe will help you understand what is going on with your many-to-many association.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have many to many relationship between employee and group. following linq statement int[]
I have two models, Project and Category, which have a many-to-many relationship between them.
I have a simple has many relationship between 2 entities in Core Data: Team
I have two tables, Books and Authors , with many-to-many relationship between them through
I have many-to-many relationship between Game and Account models like below: class Account <
I have a many-to-many relationship between employee and position. Can someone show me how
I have a many-to-many relationship between Foo and Bar Here's a simplified Foo public
I have a many-to-many relationship between two tables, let's say Friends and Foods. If
I have a many-to-many relationship between users and groups and I have a table
I have a many-to-many relationship between User s and Task s. I want the

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.