I know how to write the code in SQL, but in the EF, I'm stumped:
SQL query:
UPDATE Queue
SET Status = 'Locked'
WHERE ID = 1 AND Status = 'New'
What I've tried, but know is wrong in EF's DbContext
is this.
var queueEntities = new QueueEntities();
var queueItem = queueEntities.SingleOrDefault<Queue>(q => q.id == 1 && q.Status = "New");
queueItem.Status = "Locked";
queueEntities.Entry<Queue>(queueItem).State = System.Data.Entity.EntityState.Modified;
queueEntities.SaveChanges();
For obvious reasons, these are not the same as:
The SQL method locks the record while updating it
The DbContext
method gets it, then updates it. In concurrency, this allows Thread A to get the item and before it calls the Update, Thread B will have gotten the item, resulting in both items processing the QueueItem
Please let me know if you need something more specific.