How do you clear changes in LinqToSql?

ぐ巨炮叔叔 提交于 2019-12-02 02:21:54

问题


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:


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.




回答2:


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);   


来源:https://stackoverflow.com/questions/1158340/how-do-you-clear-changes-in-linqtosql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!