*Not* getting ChangeConflictException using Devart

谁说胖子不能爱 提交于 2019-12-11 11:32:50

问题


I'm investigating an intermittent occurrence of Devart.Data.Linq.ChangeConflictException: Row not found or changed.

I've been reading the Devart article on concurrency conflicts, and just as a starting point I am trying to reproduce a concurrency conflict. Using the included MS-based LINQ classes this is easy to do (using the code structure below does so successfully). However even following their example in the article I can't produce an exception. I have tried

  • Using both an ADO paramaterised query exactly as shown in their article
  • Using the simplified ADO query shown below (not worried about injection attacks during this test)
  • Using LinqConnect with a new context (also suggested in their article).
  • Pausing execution with a debugger and manually updating table using MySQL Workbench.

This is the test code I am using:

public static void MySqlTest()
{
    using (MySqlDataContext db = new MySqlDataContext())
    {
        Customer customer = db.Customers.First(c => c.Username.Equals("ian2"));

        MySqlAdoChange(db.Connection);
        //MySqlLinqConnectChange();

        customer.Address1 = "Original change" + DateTime.UtcNow.Ticks;
        db.SubmitChanges();
    }
}

public static void MySqlAdoChange(DbConnection connection)
{
    connection.Open();
    var command = connection.CreateCommand();
    command.CommandText = "UPDATE customers SET Address1 = 'Conflicting change" + DateTime.UtcNow.Ticks + "' WHERE Username = 'ian2'";
    command.ExecuteNonQuery();
}

public static void MySqlLinqConnectChange()
{
    using (MySqlDataContext db = new MySqlDataContext())
    {
        Customer customer = db.Customers.First(c => c.Username.Equals("ian2"));
        customer.Address1 = "Conflicting change" + DateTime.UtcNow.Ticks;
        db.SubmitChanges();
    }
}

What is doubly-strange is that the value saved to the database alternates between the two! I had to append DateTime ticks on to the end to ensure uniqueness, otherwise it was optimising away my update, and only updating to become the value that wasn't currently active.

Can anyone explain this behaviour? Why can't I produce a ChangeConflictException?


回答1:


First note that in your example code, you update Phone field in MySqlTest function, but Address1 field in MySqlAdoChange function, so there cannot be any conflict.

However, most likely even if you correct this you won't have update conflict anyway. If you look at your Customer class source code, at Phone property, you (most likely) will see it is decorated with Column attribute with UpdateCheck = UpdateCheck.Never (which is the default: see here https://www.devart.com/linqconnect/docs/MemberMapping.html). So by default, LinqConnect won't check for update conflicts for your fields, unless you tell it which fields to check by decorating them with UpdateCheck = UpdateCheck.Always or UpdateCheck.WhenChanged. Of course you should not modify source code directly but instead change that property in model designer.



来源:https://stackoverflow.com/questions/36586645/not-getting-changeconflictexception-using-devart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!