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