V2.6:Count of related collection updated when assigning parent property.

Posts   
 
    
PatrickD
User
Posts: 65
Joined: 05-Sep-2006
# Posted on: 10-Jun-2008 12:55:01   

I have two tables: DaySchedule and TimeSchedule. There is a 1:n relation between the two tables.

I have the following code:


DayScheduleEntity dayScheduleEntity = new DayScheduleEntity(4);
TimeScheduleCollection collection = dayScheduleEntity.TimeSchedule;
int oldCount = collection.Count;
TimeScheduleEntity timeScheduleEntity = new TimeScheduleEntity();
timeScheduleEntity.DaySchedule = dayScheduleEntity;
collection.Add(timeScheduleEntity);
int newCount = collection.Count;

The original count (oldCount) was 3. The new count (newCount) is 5. So the entity is added twice: First time by setting property timeScheduleEntity.DaySchedule. Second time by collection.Add.

In version 2.5 the entity was only added once to the collection. I think collection.Add() did not add the already linked entity, but I am not sure.

Is this behavior by design or is this a bug?

Note that when calling Save() on the collection the entity is not inserted twice in the database, so when rereading the collection the count is (the expected) 4.

Regards,

Patrick

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 10-Jun-2008 13:46:42   

PatrickD wrote:

I have two tables: DaySchedule and TimeSchedule. There is a 1:n relation between the two tables.

I have the following code:


DayScheduleEntity dayScheduleEntity = new DayScheduleEntity(4);
TimeScheduleCollection collection = dayScheduleEntity.TimeSchedule;
int oldCount = collection.Count;
TimeScheduleEntity timeScheduleEntity = new TimeScheduleEntity();
timeScheduleEntity.DaySchedule = dayScheduleEntity;
collection.Add(timeScheduleEntity);
int newCount = collection.Count;

The original count (oldCount) was 3. The new count (newCount) is 5. So the entity is added twice: First time by setting property timeScheduleEntity.DaySchedule. Second time by collection.Add.

In version 2.5 the entity was only added once to the collection. I think collection.Add() did not add the already linked entity, but I am not sure.

Is this behavior by design or is this a bug?

Note that when calling Save() on the collection the entity is not inserted twice in the database, so when rereading the collection the count is (the expected) 4.

Regards,

Patrick

In v2.5 it also wasn't necessary to add the entity again: myOrder.Customer = myCustomer; would add myOrder to myCustomer.Orders, this is a feature which has been present for a long time. However, it could be you did hide the relation from one side perhaps.

In v2.5, when you do: collection.Add(timeScheduleEntity); what it does is effectively this: - add the entity to the collection - setup the sync with the owner of the collection, which does: timeScheduleEntity.DaySchedule = null; timeScheduleEntity.DaySchedule = dayScheduleEntity;

due to this, the entity is removed once from the collection, so you didn't get a duplicate.

In v2.6, there's no syncing again when you set the entity twice: myOrder.Customer = myCustomer; myOrder.Customer = myCustomer; // has no effect

this has the consequence that when you do: myOrder.Customer = myCustomer; myCustomer.Orders.Add(myOrder);

it will add myOrder twice. This is discussed in Migrating your code:

If you add the same entity object twice to a collection, which is contained inside an entity, e.g. you add myOrder twice to myCustomer.Orders, it is now added twice and not as before, added once. If you set the property DoNotPerformAddIfPresent to true on the collection, the entity is added once. DoNotPerformAddIfPresent is false by default. This change is caused by the fact that if you set a property mapped onto a relation to the same object (e.g. myOrder.Customer = myCustomer; where myOrder.Customer was already set to myCustomer), it’s no longer desynced and re-synced, the set is a simple no-op, as nothing has to be done. This means that if you add myOrder twice to myCustomer.Orders, it’s no longer first desynced from myCustomer, and therefore it’s not removed from the collection before it’s added. Normally this isn’t a problem, only when you tend to add entities multiple times to a collection, have DoNotPerformAddIfPresent set to false and rely on the # of entities in the collection. To work around this: either set the flag DoNotPerformAddIfPresent to true or prevent adding the same entity twice, if you really have to rely on the # of unique entities in a collection.

ref: http://www.llblgen.com/documentation/2.6/hh_goto.htm#migratingcode.htm

Frans Bouma | Lead developer LLBLGen Pro
PatrickD
User
Posts: 65
Joined: 05-Sep-2006
# Posted on: 10-Jun-2008 13:54:58   

Okay, overlooked that one (must be the 3-0 party aftermath) flushed .

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 10-Jun-2008 14:24:19   

PatrickD wrote:

Okay, overlooked that one (must be the 3-0 party aftermath) flushed .

haha I can imagine smile (I was a bit fuzzy myself this morning as well wink )

Frans Bouma | Lead developer LLBLGen Pro
Antonio avatar
Antonio
User
Posts: 67
Joined: 09-Oct-2007
# Posted on: 10-Jun-2008 15:45:59   

Otis wrote:

PatrickD wrote:

Okay, overlooked that one (must be the 3-0 party aftermath) flushed .

haha I can imagine smile (I was a bit fuzzy myself this morning as well wink )

rage cry

BTW great match played by Oranje

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 10-Jun-2008 16:56:43   

Antonio wrote:

Otis wrote:

PatrickD wrote:

Okay, overlooked that one (must be the 3-0 party aftermath) flushed .

haha I can imagine smile (I was a bit fuzzy myself this morning as well wink )

rage cry

BTW great match played by Oranje

Sorry Antonio, most of the time our national team can't live up the high hopes the orange brigade has but last night they delivered wink

Frans Bouma | Lead developer LLBLGen Pro