structure:
TypeA has collection of TypeB and TypeB has collection of TypeC.
TypeB and TypeC are manytomany.
TypeA mapped
HasMany<TypeB>(t => t.TypeBList).CascadeAll().Inverse()..
TypeB mapped
HasManyToMany<TypeC>(t => t.TypeCList).Casecade.All().Inverse()..
TypeC mapped
HasManyToMany<TypeB>(t => t.TypeBList).Casecade.None()...
TypeB has a method to
Add(TypeC typec) { typec.TypeBList.Add(this); TypeBList.Add(typec); }
And I need to add a new typec to typeb collection, but
I want to save by calling repo.Save(typeA) but I’m getting shared collection issue because TypeB exists in both TypeA and TypeC.
var typec = new TypeC { TypeBList = typea.TypeBList, .... }
typea.TypeBList[0].Add(typec);
repo.save(typea);
Will this not work by saving typea, am I forced to get a typeb and make the changes there?
collections on objects should generally not be shared at all and most of the time, especially when using NHibernate, you shouldn’t even alter the collection instance. better copy the contents.
Update: Why?
Because NHibernate replaces the collection instances with own collection classes to enable lazyloading and changetracking.