Posted by: Cirilo Meggiolaro | 10/17/2008

Tip of the day #3 – Getting details of a LINQ change conflict exception

The ChangeConflictException happens when the application tries to update values that have been changed after LINQ have read them.

There’s an easy way to get a list of tables and columns that have thrown the exception. The code below catches the exception and loop through conflict objects to generate a full report:

MonitorDataContext ctx = new MonitorDataContext();

// Some code…
foreach (ObjectChangeConflict changeConflict in ctx.ChangeConflicts)
MetaTable metatable = ctx.Mapping.GetTable(changeConflict.Object.GetType());
customer entityInConflict = (customer)changeConflict.Object;

Console.WriteLine(“Table name: {0}”, metatable.TableName);
Console.WriteLine(“Item ID: {0}”, entityInConflict.customer_id);

foreach (MemberChangeConflict col in changeConflict.MemberConflicts)
Console.WriteLine(“Column name : {0}”, col.Member.Name);
Console.WriteLine(“Original value : {0}”, col.OriginalValue.ToString());
Console.WriteLine(“Current value : {0}”, col.CurrentValue.ToString());
Console.WriteLine(“Database value : {0}”, col.DatabaseValue.ToString());

Code 1 – Iterating over change conflict objects

For an overview about optimistic concurrency, click here!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: