none
C# LINQ-TO-SQL: datacontext.ChangeConflicts not detects all conflicts RRS feed

  • Question

  • I am running into problem when working with linq-to-sql and trying to resolve conflicts. The problem is that sometimes conflict is not detected. Please look on the below code sample:

    // Setup the object to re-produce the problem
    //
    // MyObject has properties: id, my_string, my_int and version (timestamp) to enable
    // conflicts detection
    var context = new MyDataContext();
    var obj = new MyObject();
    obj
    .id = "1";
    obj
    .my_string = "value";
    obj
    .my_int = 0;
    context
    .MyTable.InsertOnSubmit(obj);
    context
    .SubmitChanges();
    context
    .dispose();

    // Get 2 data contexts
    var context1 = new MyDataContext();
    var context2 = new MyDataContext();

    // Get 2 instances of obj - 1 from each context
    var obj1= context1.MyTable.SingleOrDefault(o => o.id == "1");
    var obj2= context2.MyTable.SingleOrDefault(o => o.id == "1");

    // Change the values of obj1 and update it to the database
    obj1
    .my_string= "value1";
    obj1
    .my_int= 1;
    context1
    .SubmitChanges();
    context1
    .Dispose();

    // Update the changes in obj2
    obj2
    .my_string= "value2";
    obj2
    .my_int= 0;

    // Now the database contains:
    // id: "1"
    // my_string: "value1"
    // my_int: 1

    // obj2 contains:
    // id: "1"
    // my_string: "value2"
    // my_int: 0

    try
    {
    context2
    .SubmitChanges();
    }
    catch(ChangeConflictException ex)
    {
    LogInfo("Conflicting members:");
    context2
    .ChangeConflicts[0].MemberConflicts.ToList().ForEach(
    mcc
    =>LogInfo("Property '{0}': Database value: {1}, Current Value:{2}", mcc.Member.Name, mcc.DatabaseValue, mcc.CurrentValue)
    );
    }
    context2
    .Dispose();

    In the catch I expects to see 3 member conflicts: my_string, my_int and version but I see only 2 member conflicts: my_string and version. The my_int is not detected as conflict.

    If I would have setup the my_int when the object was created to value different from the value that I have assigned to obj2, the conflict is being detected.

    I found some commonality: when the value of a property (any property) of the original object is equal to the value of obj2, the conflict for this property is not detected.

    I would like to get any idea how to overcome this problem so conflicts will successfully detected

    Sunday, May 8, 2011 5:41 AM

Answers

  • Hello Koby,

    Welcome to the EF Forum.

     

    According to your description, I reproduced your scenario. I found that using your code, I didn’t detect all the conflicts, so I modified a little, and then I got them. The following is the try-catch block I modified:

      try

               {

                   context2.SubmitChanges();

               }

               catch (ChangeConflictException ex)

               {

                   Console.WriteLine("Optimistic concurrency error.");

                   Console.WriteLine(ex.Message);

                  

                   var occ = context2.ChangeConflicts[0].MemberConflicts.ToList();

                   foreach (MemberChangeConflict mcc in occ)

                   {

                       object currVal = mcc.CurrentValue;

                       object origVal = mcc.OriginalValue;

                       object databaseVal = mcc.DatabaseValue;

                       MemberInfo mi = mcc.Member;

                       Console.WriteLine("Member: {0}", mi.Name);

                       Console.WriteLine("current value: {0}", currVal);

                       Console.WriteLine("original value: {0}", origVal);

                       Console.WriteLine("database value: {0}", databaseVal);

                   }

                   Console.ReadLine();

               }

              

               context2.Dispose();

    The result is in the image as below:

    http://cid-9309437b6807d073.photos.live.com/self.aspx/New%20album/result.jpg

     

    I hope this can help you.

     

    Have a nice day,

     


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.




    Tuesday, May 10, 2011 5:18 AM
    Moderator