DoNotPerformAddIfPresent

Posts   
 
    
eugene
User
Posts: 85
Joined: 05-Oct-2004
# Posted on: 26-May-2005 15:26:41   

Hi there,

as I understand it, DoNotPerformAddIfPresent will prevent entities with the same PK definition of being added to an EntityCollection.

I did the following test with an entity

            
Me.Auftragsposition.AuftragsPosDokumente.DoNotPerformAddIfPresent = True
_auftragsPosDoc = New MyAuftragsPosDokumenteEntity(Me.Auftragsposition.AuftragsNr, Me.Auftragsposition.Auftragsposition, 14)
_auftragsPosDocTmp = New MyAuftragsPosDokumenteEntity(Me.Auftragsposition.AuftragsNr, Me.Auftragsposition.Auftragsposition, 14)
Me.Auftragsposition.AuftragsPosDokumente.Add(_auftragsPosDoc)
Me.Auftragsposition.AuftragsPosDokumente.Add(_auftragsPosDocTmp)

As you can see I am using the PK constructor of the entity and after the operation I see two identical elements in the collection. - Am I missing on something?

Best regards

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 26-May-2005 16:31:46   

Set IsNew to false and try again. Equals checks the PK values when the entity is not new, otherwise an instance check is performed.

Frans Bouma | Lead developer LLBLGen Pro
eugene
User
Posts: 85
Joined: 05-Oct-2004
# Posted on: 26-May-2005 17:41:33   

Dear Otis,

thank you for your reply!

Added new entites already have a PK set, is it possible to have the EntityCollection proof these elements? In my case, entites are being added by a grid through databinding (NewLine function) and not in code. I tried removing these entities by hand but this is causing a lot of troubles with the various events being fired. Could it be possible to do this test on new objects? Can you suggest any workaround?

Best regards

eugene
User
Posts: 85
Joined: 05-Oct-2004
# Posted on: 26-May-2005 17:49:29   

What I forgot to say is: setting IsNew to false works (of course). However, wouldn't setting isNew to false prevent the Adapter from attempting to save these entities using an Insert and it would instead try an update, Am i right on this? Also, the elements are added via the grid, so I have no control on the elements before they are added.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 27-May-2005 11:15:25   

You can also test if the PK is already there and if not, add the entity. Here's an example:


// newCustomerEntity is filled with data, but new, now check if it's already in the collection.
newCustomerEntity.IsNew = false;
if(customers.IndexOf(newCustomerEntity)<0)
{
// not there yet.
newCustomerEntity.IsNew = false;
customers.Add(newCustomerEntity);
}

Frans Bouma | Lead developer LLBLGen Pro