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

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

    Excellent write up on here, but here is a copy and paste of the code used.

    Public Sub DiscardInsertsAndDeletes(ByVal data As DataContext)
        ' Get the changes
        Dim changes = data.GetChangeSet()
    
        ' Delete the insertions
        For Each insertion In changes.Inserts
            data.GetTable(insertion.GetType).DeleteOnSubmit(insertion)
        Next
    
        ' Insert the deletions
        For Each deletion In changes.Deletes
            data.GetTable(deletion.GetType).InsertOnSubmit(deletion)
        Next
    End Sub
    
    Public Sub DiscardUpdates(ByVal data As DataContext)
        ' Get the changes
        Dim changes = data.GetChangeSet()
    
        ' Refresh the tables with updates
        Dim updatedTables As New List(Of ITable)
        For Each update In changes.Updates
            Dim tbl = data.GetTable(update.GetType)
            ' Make sure not to refresh the same table twice
            If updatedTables.Contains(tbl) Then
                Continue For
            Else
                updatedTables.Add(tbl)
                data.Refresh(RefreshMode.OverwriteCurrentValues, tbl)
            End If
        Next
    End Sub
    

提交回复
热议问题