Whats the best way to move a child of a collection to another collection?
var newX = x.Clone();
xCollection.Add(newX);
OR
var newX = xCollection.AddNew();
but what to do with x?
This is in the FAQ: http://www.lhotka.net/cslanet/faq/CslaObjectFaq.ashx
Thanks.
I'm actually doing a copy. Is it true that Add(X x) and AddNew() differ in that 'AddNew' marks the collection as IsDirty and 'Add' doesn't?
var newX = x.Clone();
owner.xCollection.Add(newX);
owner is NOT marked IsDirty (xCollection is a CSLA property of owner).
AND
owner.xCollection.AddNew();
owner IS marked as IsDirty
Add and AddNew are very different.
AddNew causes AddNewCore to execute, which creates a new object and adds it to the collection. This doesn't add your existing object - it creates a new one. Since the new object is actually new, IsDirty and IsNew are both true - so the collection is dirty.
Add accepts a "new" object as a parameter and adds that object to the collection. If the new object has IsDirty=false, then the collection won't be dirty.
The IsDirty property of a collection is entirely dependent on whether any of its child objects are dirty.
"The IsDirty property of a collection is entirely dependent on whether any of its child objects are dirty."
Thanks, that's the bit I missed!
However, I noticed that when I removed a child form the collection the collection became IsDirty
How come?
This is because the removed child is marked for deletion (so IsDeleted and IsDirty are true) and it is moved to DeletedList. When that collection is saved, that child is deleted from the database.
Copyright (c) Marimer LLC