Entity Framework conditional update based on timestamp RRS feed

  • Question

  • Im working on a system that takes updates via web services from a backend system (AS400). Each of these updates have a timestamp on them. The updates could be the following: Create, Update, Delete etc. So, it is possible for a combonation of these operations to take place on a single record on the backend system, but arrive in my system out of order (IE 2 updates for the same record arrive out of order so dirty writes are possible). We have overcome this issue by using the timestamps and custom stored procedures. The 'Create / Update' stored procedure compares the incoming timestamp against the timestamp on the row being updated, and performs an insert or update appropriately. The 'Delete' stored procedure compares the incoming timestamp to the record in the database and conditionally deletes the record (or 'soft deletes' the record incase an out of order insert arrives later). This has worked well, but I would like to get away from writing custom data access code if possible. 

    My question is this: is there a way to have EF insert / update or delete based on a timestamp, or is there a way to extend the functionality such that you can add this custom check? The only place where this type of check can take place and be threadsafe is in the database, as we are using a cluster of web servers, so these messages can come in on different servers, but they're all talking to the same database.

    Thanks in advance.


    Friday, July 8, 2011 1:58 PM


  • Hi!

    In EF you can on each property on the entity define concurrency mode, so just mark your timestamp columns as Concurrency mode as fixed, and you will have a working optimistic locking.

    Hope this answers your question!

    • Marked as answer by Alan_chen Tuesday, July 26, 2011 5:52 AM
    Friday, July 8, 2011 2:23 PM

All replies