[FIXED] Adding new entities with Identity key to collections fails

Posts   
 
    
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39872
Joined: 17-Aug-2003
# Posted on: 24-Nov-2003 11:59:11   

** Description: ** When you try to add more than one new entity to an entity collection and that entity has a primary key which consists solely of an identity field (sequenced field), the first addition will succeed, but the second and all additions of a new entity to that collection will fail because the Add() method thinks the new entity is already present in the collection.

Cause: EntityCollection.Add(newEntity) performs an IList.Contains(newEntity) to check whether the new entity is already present in the collection. Contains calls for each entity Equals(newEntity) to check if it's equal to the new entity to be inserted. Equals() compares, if applicable, the primary key values of the current entity with the entity to compare with. When all primary key values are equal, a match is found. If no primary key fields are present, all fields are compared. Because the new entity and the other new entity have an identity primary key field, these fields do not yet have a proper value (because these values are assigned when the entity is inserted), and are 0. This means that the second and other new entities match with the first added new entity: both have the value 0 for the primary key field.

Bug location: ORM Support classes.

Solution: A fix is in the works to fix this problem.

Workaround: Instead of adding new entities to an entity collection and call SaveMulti(), use a transaction object and save the new entities by hand.

Frans Bouma | Lead developer LLBLGen Pro
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39872
Joined: 17-Aug-2003
# Posted on: 25-Nov-2003 11:16:37   

FIXED in the update for the ORM support classes library released today.

Frans Bouma | Lead developer LLBLGen Pro