Sunday, May 29, 2005

[Opf3] Delete objects that are part of a loaded ObjectSet

How to delete objects that are part of a loaded ObjectSet<T>? The ObjectSet contains a list that is called the "RemovedList" (there is a property that is also named like that).

All objects that are removed from the ObjectSet by using the RemoveAt, Remove, RemoveAll methods are moved to the RemovedList. When the ObjectSet is saved all items on the RemovedList are deleted.


ObjectSet<User> os = context.GetObjectSet<User>("UserName like {0}", "Jason%");

// Remove the first object from the ObjectSet.
// The object is moved to the RemovedList.
os.RemoveAt(0);
// Returns exactly that item.
User user = os.RemovedList[0];

// Saves all changes to the objects and deletes
// the objects in the RemovedList.
context.PersistChanges(os);


How to remove an item from the ObjectSet without deleting it. You have to move it to the RemovedList and remove then from there. It's a little bit complicated but not required very often.


// Move the first item to the RemovedList.
os.RemoveAt(0);
// Remove it from there. The item is now no longer
// saved nor deleted.
os.RemovedList.RemoveAt(0);

0 Comments:

Post a Comment

<< Home