How to: Manage Data Concurrency in the Object Context

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

This topic provides an example of how to manage concurrency in an object context. In this topic, you will generate and handle an OptimisticConcurrencyException when updating the Status property of SalesOrderHeader object. For more information, see Saving Changes and Managing Concurrency.

The example in this topic is based on the Adventure Works Sales Model. To run the code in this example, you must have already added the AdventureWorks Sales Model to your project and configured your project to use the Entity Framework. To do this, complete the procedures in How to: Manually Configure an Entity Framework Project and How to: Manually Define the Model and Mapping Files.

To successfully generate an OptimisticConcurrencyException, you must modify the Status property in the conceptual mapping file.

To enable concurrency checking on the Status property

  1. Open the AdventureWorks.csdl file and locate the definition for the SalesOrderHeader entity.

  2. Find the child Status element, and add the following attribute:

  3. Save changes to AdventureWorks.csdl.

  4. In the following example code, set a breakpoint after the foreach loop (For Each in Visual Basic) and run the application in debug mode.

  5. When the execution breaks, use SQL Server Management Studio to execute the following Transact-SQL command against the AdventureWorks database:

    UPDATE Sales.SalesOrderHeader SET Status = 1 WHERE CreditCardApprovalCode IS NULL.
  6. Restart program execution.


In this example, changes to the Status property of the SalesOrderHeader object cause an OptimisticConcurrencyException when the procedure above is followed.

See Also