EntityCollection strange behaviour

Posts   
 
    
omar avatar
omar
User
Posts: 569
Joined: 15-Oct-2004
# Posted on: 07-Mar-2005 20:14:03   

Greetings, I have two collections that both use the same type of entity factory (NOT the same entity factory object!!) and I want to copy the entities from one of the collections to the other

 Dim state As EntityBase2 = DirectCast(m_state, EntityBase2)
For i As Integer = 0 To Me.GetMemberEntityCollections.Count - 1

            Dim myCol As EntityCollectionBase2 = DirectCast(Me.GetMemberEntityCollections(i), EntityCollectionBase2)

            Dim stateCol As EntityCollectionBase2 = DirectCast(state.GetMemberEntityCollections(i), EntityCollectionBase2)

            myCol.Clear()

            Do While stateCol.Count > 0
                myCol.Add(stateCol(0))
            Loop

        Next

I had to write the code copying entities between the two collections (myCol and stateCol) because I noticed in debugging that as an entity is added to (myCol) it actually gets removed from (stateCol). For this particular situation thats OK with me but I wanted to know if this behaviour is by design or am I missing something ?? flushed

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 08-Mar-2005 10:18:03   

The reason it gets removed from one collection is this: say I have two customer objects, cust1 and cust2. I have one order object, myOrder. myOrder is in cust1.Orders. This means that myOrder and cust1 are related and that myOrder.Customer is cust1.

When I add myOrder to cust2.Orders, I effectively assign myOrder to cust2, which means I have to dereference cust1 as the customer, because cust2 is now the customer for myOrder. This means that myOrder is removed from the collection cust1.Orders.

This behavior is performed if the collection you add to is contained in an entity. If you add to a new collection, it isn't removed as that addition doesn't mean an assignment to a related entity.

Frans Bouma | Lead developer LLBLGen Pro
omar avatar
omar
User
Posts: 569
Joined: 15-Oct-2004
# Posted on: 08-Mar-2005 16:33:51   

Thanks for the answer.. all is clear now...