- Posted by liammclennan on July 6, 2011
1. Ideally, a many-to-many relationship should be managed by just one end of the relationship. Therefore, the mapping on one end should be inverse. If both ends control the relationship then connecting the two entities can cause duplicate entries in the join table.
2. To delete an entiity that is part of a many-to-many the entity must be removed from the collection that controlls the relationship (i.e. the one that is not inverse)
3. Cascading on many-to-many relationships refers to the other entity, not to the relationship. Cascading deletes is usually a bad idea.
4. By default, fluent nhibernate maps many-to-many relationships using bags. If something is deleted from the bag nhibernate persists the change by deleting (from the database) every record in the bag, and then re-inserting every record except the one that was deleted. If the collection contains a large number of records this could cause a performance problem. A solution is to map the relationship as sets. For fluent nhibernate this means using the AsSet() method on the relationship mapping and using ICollection<> as the type of the collections.