views:

270

answers:

2

I have a combobox in my WPF app that is databound to my list of objects in my View Model. When the user makes changes to the selected object and then selects another item before saving, I need to clear the changes made.

I thought I could use dataContext.GetChangeSet().Updates.Clear() but for some reason the collection is read-only.

I've also tried to use dataContext.Refresh but this doesn't work either as the object doesn't exist in the database, I created it manually from an SP.

Please help. thanks.

+1  A: 

Your best bet is probably to re-query into a separate data-context. You can negate an insert (from the change-set) by using DeleteOnSubmit (and the reverse), but I'd rather not, myself.

Marc Gravell
so are you saying to basically create a new datacontext and go get the data again? If so then this is very slow as my collection has loads of items. I was hoping there would be a copy of the object held somewhere so that any changes could be rolled back (like an undo feature).
HAdes
There is an old version in the change tracker; but the idea of a data-context is for it to be a unit-of-work; not a database. IMO, it sounds like you're simply loading too much data into it to start with...
Marc Gravell
oh ok so there is an old version that i can use, where is it please? I take your point about loading too much, but i can't see an alternative, thanks.
HAdes
GetOriginalEntityState() on each table
Marc Gravell
A: 

As well as using Marc's approach using DeleteOnSubmit (or DeleteAllOnSubmit) to remove the inserts, the following will actually undo any updates aswell:

// clears any updates.  
ChangeSet changes = dataContext.GetChangeSet();
dataContext.Refresh(RefreshMode.OverwriteCurrentValues, changes.Updates);   
HAdes