Export (0) Print
Expand All

How to: Display a ChangeSet

You can view changes tracked by a DataContext by using GetChangeSet.

The following example retrieves customers whose city is London, changes the city to Paris, and submits the changes back to the database.

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();

Output from this code appears similar to the following. Note that the summary at the end shows that eight changes were made.

CustomerID: AROUT

    Original value: London

    Updated value: Paris

CustomerID: BSBEV

    Original value: London

    Updated value: Paris

CustomerID: CONSH

    Original value: London

    Updated value: Paris

CustomerID: EASTC

    Original value: London

    Updated value: Paris

CustomerID: NORTS

    Original value: London

    Updated value: Paris

CustomerID: PARIS

    Original value: London

    Updated value: Paris

CustomerID: SEVES

    Original value: London

    Updated value: Paris

CustomerID: SPECD

    Original value: London

    Updated value: Paris

 

Total changes: {Added: 0, Removed: 0, Modified: 8}

Show:
© 2014 Microsoft