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

后端 未结 10 1031
礼貌的吻别
礼貌的吻别 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:28

    You can use the GetOriginalEntityState(..) to get the original values for the objects e.g. Customers using the old cached values.

    You can also iterate through the changes e.g. updates and refresh the specific objects only and not the entire tables because the performance penalty will be high.

    foreach (Customer c in MyDBContext.GetChangeSet().Updates)
            {
                MyDBContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, c);
            }
    

    this will revert the changes using persisted data in the database.

    Another solution is to dump the datacontext you use, using Dispose().

    In any case it is a good practice to override the Insert and Remove methods in the collection of e.g. Customers you use and add e.g. an InsertOnSubmit() call. This will resolve your issue with pending Insertions and Deletions.

提交回复
热议问题