BAD_SEED
BAD_SEED

Reputation: 5056

Linq-To-Sql: Why my changeset is always zero?

I have a query similar to the one on msdn site:

// Query the database for the row to be updated. 
var query =
    from ord in db.Orders
    where ord.OrderID == 11000
    select ord;

// Execute the query, and change the column values 
// you want to change. 
foreach (Order ord in query)
{
    ord.ShipName = "Mariner";
    ord.ShipVia = 2;
    // Insert any additional changes to column values.
}

// Submit the changes to the database. 
try
{
    db.SubmitChanges();
}
catch (Exception e)
{
    Console.WriteLine(e);
    // Provide for exceptions.
}

What I want now, is a way to know the affected rows of last update command. I've tried using:

int affectedRows = dc.GetChangeSet().Updates.Count;

in various manner, but this instruction always getting me 0, even if the table is correctly updated.

Upvotes: 2

Views: 261

Answers (1)

StriplingWarrior
StriplingWarrior

Reputation: 156624

dc.GetChangeSet() tells you how many changes your LINQ to SQL context is planning to make when you call SubmitChanges(). It does not track the number of affected rows as reported by the database.

If you call int affectedRows = dc.GetChangeSet().Updates.Count; before calling SubmitChanges, you will see how many rows it expects to have affected. After calling SubmitChanges, there are no more pending changes, so you'll always get a zero count.

Upvotes: 3

Related Questions