I am trying to implement a application following the sample in this page: http://www.asp.net/entity-framework/tutorials/handling-concurrency-with-the-entity-framework-in-an-asp-net-mvc-application
I have a domain class with Timestamp as the concurrency check field:
public class PurchaseOrder {
[Timestamp]
public byte[] Timestamp {get; set;}
}
In my Edit.aspx I have the Timestamp as hidden field (I am using a view model):
<%: Html.HiddenFor(model => model.PurchaseOrder.Timestamp) %>
This is my Edit() method:
public ActionResult Edit(int id, FormCollection collection) {
var purchaseOrder = db.PurchaseOrders.Find(id);
UpdateModel(purchaseOrder, "PurchaseOrder", collection);
db.Entry(purchaseOrder).State = EntityState.Modified;
db.SaveChanges();
}
I opened the same edit page in 2 separate browser at the same time (so that their timestamp is the same), and update them one after the other.
When I update the second page, I expected a DbUpdateConcurrencyException. But I am getting none.
What I think happened is that in the second page, I am getting the purchaseOrder object again from the DB in the Edit action:
var purchaseOrder = db.PurchaseOrders.Find(id);
So the timestamp is the new timestamp because of the previous edit.
But I expected the UpdateModel() to replace the Timestamp value from the formcollection. Obviously, this is not the case.
How can I set the value of the Timestamp of the retrieved purchaseOrder to the in the hidden field, so that the concurrency will be detected?