Kevin Pang
Kevin Pang

Reputation: 41442

NHibernate Many-To-Many Is Deleting All Associations Before Inserting

I have a Users table and a Networks table with a many-to-many relationship between them (a user may be long to multiple networks and a network may contain many users). The many-to-many relationship is held in a "UserNetworks" table that simply has two columns, UserId and NetworkId.

My classes look like this:

public class User
{
    public IList<Network> Networks {get; set;}
}

public class Network
{
    public IList<Usre> Users {get; set;}
}

The NHibernate mappings for these many-to-many collections looks like this:

User.hbm.xml:

<bag name="Networks" table="UserNetworks" cascade="save-update" inverse="true">
    <key column="UserId" />
    <many-to-many class="Network" column="NetworkId" />
</bag>

Network.hbm.xml:

<bag name="Users" table="UserNetworks" cascade="save-update">
    <key column="NetworkId" />
    <many-to-many class="User" column="UserId" />
</bag>

In my code, I create an association between a user and a network like so:

user.Networks.Add(network);
network.Users.Add(user);

I would expect the SQL run to simply perform one INSERT to the UserNetworks table. Instead, it executes a DELETE on the UserNetworks table with NetworkID = X, then proceeds to reinsert all the UserNetworks rows back in along with the new association.

What am I doing wrong?

Upvotes: 4

Views: 2021

Answers (1)

o.k.w
o.k.w

Reputation: 25810

The behaviour is by design. See, NH do not know what's current/new/deleted in the list. So by deleting all and inserting whatever in the list will make sense.

Upvotes: 5

Related Questions