How can I reject all changes in a Linq to SQL's DataContext?

后端 未结 10 971
礼貌的吻别
礼貌的吻别 2020-11-27 18:57

On Linq to SQL\'s DataContext I am able to call SubmitChanges() to submit all changes.

What I want is to somehow reject all changes in the datacontext and rollback a

10条回答
  •  醉梦人生
    2020-11-27 19:23

    My application is outlook style with a icon to select an active form (ListBox). Before allowing the user to change their context they have to accept changes or discard them.

    var changes = db.GetChangeSet();
    if ((changes.Updates.Count > 0) || (changes.Inserts.Count > 0) || (changes.Deletes.Count > 0))
    {
        if (MessageBox.Show("Would you like to save changes?", "Save Changes", MessageBoxButton.YesNo)  == MessageBoxResult.Yes)
        {
            db.SubmitChanges();
        } else
        {
            //Rollback Changes
            foreach (object objToInsert in changes.Inserts)
            {
                db.GetTable(objToInsert.GetType()).DeleteOnSubmit(objToInsert);
            }
            foreach (object objToDelete in changes.Deletes)
            {
                db.GetTable(objToDelete.GetType()).InsertOnSubmit(objToDelete);
            }
            foreach (object objToUpdate in changes.Updates)
            {
                db.Refresh(RefreshMode.OverwriteCurrentValues, objToUpdate);
            }
            CurrentForm.SetObject(null); //Application Code to Clear active form
            RefreshList(); //Application Code to Refresh active list
        }
    }
    

提交回复
热议问题