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

后端 未结 10 758
礼貌的吻别
礼貌的吻别 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
    
    0 讨论(0)
  • 2020-11-27 19:27

    In .net 3.0 use the db.GetChangeSet().Updates.Clear() for updated, db.GetChangeSet().Inserts.Clear() for new or db.GetChangeSet().Deletes.Clear() for deleted items.

    In .net 3.5 and above the result of GetChangeSet() is now readonly, loop the collection in for or foreach and refresh every ChangeSet table like also macias wrote in his comment.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-27 19:28

    Here is how I did it. I just followed Teddy's example above and simplified it. I have one question though, why even bother with the refresh on the DELETES?

      public static bool UndoPendingChanges(this NtsSuiteDataContext dbContext)
      {
         if (dbContext.ChangesPending())
         {
            ChangeSet dbChangeSet = dbContext.GetChangeSet();
    
            dbContext.Refresh(RefreshMode.OverwriteCurrentValues, dbChangeSet.Deletes);
            dbContext.Refresh(RefreshMode.OverwriteCurrentValues, dbChangeSet.Updates);
    
            //Undo Inserts
            foreach (object objToInsert in dbChangeSet.Inserts)
            {
               dbContext.GetTable(objToInsert.GetType()).DeleteOnSubmit(objToInsert);
            }
    
            //Undo deletes
            foreach (object objToDelete in dbChangeSet.Deletes)
            {
               dbContext.GetTable(objToDelete.GetType()).InsertOnSubmit(objToDelete);
            }
         }
    
         return true;
      }
    
    0 讨论(0)
提交回复
热议问题