ChangeConflictException in Linq to Sql update

后端 未结 4 625
感动是毒
感动是毒 2020-12-14 19:57

I\'m in a world of pain with this one, and I\'d very much appreciate it if someone could help out.

I have a DataContext attached to a single test table on a database

相关标签:
4条回答
  • 2020-12-14 20:19

    First log details about the problem, what row and what field is in conflict and what values are in conflict.

    To implement such detail log, see my solution here:

    What can I do to resolve a "Row not found or changed" Exception in LINQ to SQL on a SQL Server Compact Edition Database?

    0 讨论(0)
  • 2020-12-14 20:25

    I finally figured out what was happening with this. Apparently, the "no count" option was turned on for this server.

    In Microsoft SQL Server Management Studio 2005:

    1. Right click on the server and click Properties
    2. On the left hand of the Server Properties window, select the Connections page
    3. Under Default connection options, ensure that "no count" is not selected.

    Apparently, LINQ to SQL uses @@ROWCOUNT after updates to issue an automated optimistic concurrency check. Of course, if "no count" is turned on for the entire server, @@ROWCOUNT always returns zero, and LINQ to SQL throws a ConcurrencyException after issuing updates to the database.

    This isn't the only update behavior LINQ to SQL uses. LINQ to SQL doesn't perform an automated optimistic concurrency check with @@ROWCOUNT if you have a TIMESTAMP column on your table.

    0 讨论(0)
  • 2020-12-14 20:32

    I had the same issue with SQL Server 2008 and the connection option no count already turned of.

    Instead of changing the Update Check property to Never (as Quintin suggests), I set it to WhenChanged and the issue was solved.

    0 讨论(0)
  • 2020-12-14 20:41

    Is it possible that any of the data for the row has changed between when it was retrieved and the update was attempted? Because LINQ->SQL has automatic concurrency checking that will validate the contents of the object against the currently stored values (like you see in the generated query). If it is possible that any of the fields have changed for the row in the DB vs the object LINQ is tracking then the update will fail. If this is occurring and for good reason and you know what fields, you can update the object in the DBML designer; select the field at cause and change the "Update Check" property to "Never".

    0 讨论(0)
提交回复
热议问题