Jazz wrote:
1) using Object reference, yes
2) actually "taking out" should be "taking a look at"... nothing to remove here
3) before changing any parent-references. i bind the collection to a treeview, so for my understanding all objects should have been fetched. but now i think i got your point.
when I move at the top collection level (talking about hierarchy) +1 level down/up, everything is okay, but when it comes to get the father of a father the objects get refetched from the database?
You fetch 1, 2, 3 and 4. All of them don't have their parent set, as you just fetch a non-hierarchical set of entities. Now, you set 2.Father to 4. This is great, but 3.Father is still null/not read. You then do: 3.Father.Father.
This first hits the property of 3: 3.Father. As it's not read yet, it will be read from the db through lazy loading. 3.Father is 2 in the database, so a new 2 entity is read. This new 2 entity doesn't have its Father object set (the 2 object in your collection does) and this 2.Father will load itself from the db as well.
To avoid this, add the collection you're fetching to a Context. Using a Context, you can have uniquing, which means if you load an entity twice, it's actually loaded once and you have 2 references to the same object. As soon as you add the collection to a Context, 3.Father will still load itself from the db, but because 3 is in a context, it will check if teh loaded related entity, 2, is in the context. It is, so you get the 2 instance in the collection (which is in the context) set as 3.Father, and that 2 instance has its father set to 4.
Please see the Context documentation in the manual, it will solve your problem