DataContext.GetChangeSet Method
Provides access to the modified objects tracked by DataContext.
Assembly: System.Data.Linq (in System.Data.Linq.dll)
Note the following considerations:
-
GetChangeSet might have side effects, such as inference of insert and delete operations that are usually performed at the time of SubmitChanges. For example, objects that are used in the following operations can create corresponding inferred operations in the following list:
-
Add to InsertOnSubmit.
-
EntityRef<TEntity> assignment to null (possibly because of Remove to DeleteOnSubmit.
-
-
The set may not be ordered according to foreign key constraints.
-
Database-generated values (for example, primary and foreign key values, timestamps, and so forth) are not available. Such information requires database command execution and perhaps the propagation of retrieved information (for example, foreign key from primary key).
-
The set of changed objects is computed at the time of the call. Subsequent calls to SubmitChanges can produce a different set if additional changes are made.
Output when no changes have been made appears as follows:
{Added: 0, Removed: 0, Modified: 0}
Northwnd db = new Northwnd(@"c:\northwnd.mdf"); var custQuery = from cust in db.Customers where cust.City == "London" select cust; foreach (Customer custObj in custQuery) { Console.WriteLine("CustomerID: {0}", custObj.CustomerID); Console.WriteLine("\tOriginal value: {0}", custObj.City); custObj.City = "Paris"; Console.WriteLine("\tUpdated value: {0}", custObj.City); } ChangeSet cs = db.GetChangeSet(); Console.Write("Total changes: {0}", cs); // Freeze the console window. Console.ReadLine(); db.SubmitChanges();
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Something that I had to learn the hard way, if you ever wanted to remove an object from the changeSet, such as adding a new record and the user wishes to undo the operation even before hitting save (i.e. submitting changes to db). Simply issue a DeleteOnSubmit over that object. "LINQ to SQL is smart enough to know that if a row is inserted and then deleted (or the other way around) nothing needs to be done" Check this article http://graemehill.ca/discard-changes-in-linq-to-sql-datacontext
- 9/6/2011
- Rula Antoun