Howto undo ChangeSet in LINQtoSQL

2019-05-05 08:44发布

in my data layer class I have created a function to manually refresh the data source.

Public Sub DiscardAllChanges()

    _Context.Refresh(RefreshMode.OverwriteCurrentValues)

End Sub

The problem is that the context ChangeSet after this operation still trace the previous operation of Insertion, Deletion and Update that I made calling manually InsertOnSubmit, etc.

Is it possible to clear also the ChangeSet in some way? Or if not can you suggest me another solution? Do I have to create a ChangeSet layer in the Business?

3条回答
Luminary・发光体
2楼-- · 2019-05-05 09:13

dc.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, item);

查看更多
\"骚年 ilove
3楼-- · 2019-05-05 09:18

Sounds like your trying to deal with concurrency issues? This is where when you submit, the database has already changed which causes your submit to fail. Let me know if this is what your after:

DataClasses1DataContext dc = new DataClasses1DataContext();
try
{
    dc.SubmitChanges(System.Data.Linq.ConflictMode.ContinueOnConflict);
}
catch (System.Data.Linq.ChangeConflictException ex)
{
    foreach (var conflict in dc.ChangeConflicts)
    {
        conflict.Resolve(System.Data.Linq.RefreshMode.KeepChanges);
    }
    dc.SubmitChanges();
}

This pattern does the heavy lifting for you on concurrency issues. The RefreshMode has overloads to either force your change in, refresh the latest values EXCEPT your change, or let the database overwrite your changes. You can then submit again. Note that committing in full is a recursive process, your subsequent call to submit changes can also fail.

查看更多
Deceive 欺骗
4楼-- · 2019-05-05 09:20

The easiest way to handle this is to treat the DataContext as your unit of work. Perform an atomic set of related operations on a single DataContext, then after either success or failure, Dispose() it (ideally via using) and throw it away.

Start you next set of operations on a new DataContext. Likewise, you can re-query for data.

On an object-by-object basis, you can use GetOriginalEntityState (on the table) to get the original values, but you'd need to re-apply to a "live" object, and this doesn't handle the delete/insert cases.

查看更多
登录 后发表回答