0

Environment Entity Framework 4; Visual Web Developer Express 2010; Database first; SQL Server Express

Question

I want to fetch a row from the database, update one field and save back to the database. Why does the following method throw an OptimisticConcurrencyException?

public static void SetReturnVisit(int firstAppointmentSlotID, int returnAppointmentSlotID) {
    AppointmentSlot slot = m_db.AppointmentSlots.SingleOrDefault(m => m.AppointmentSlotID == firstAppointmentSlotID);
    slot.ReturnAppointmentSlotID = returnAppointmentSlotID;
    m_db.SaveChanges();       
}

m_db is my ObjectContext Entities class.
AppointmentSlot database table contains a Timestamp field named Concurrency.
Concurrency property on entity configured as below:

  • StoreGeneratedPattern: Computed
  • Concurrency Mode: Fixed

I am working as a single developer, so nothing else is modifying the record.

I have tried various approaches, like detaching the item, updating field and attaching again.

public static void SetReturnVisit(int firstAppointmentSlotID, int returnAppointmentSlotID) {
    AppointmentSlot slot = m_db.AppointmentSlots.SingleOrDefault(m => m.AppointmentSlotID == firstAppointmentSlotID);
    m_db.AppointmentSlots.Detach(slot);
    slot.ReturnAppointmentSlotID = returnAppointmentSlotID;
    m_db.AppointmentSlots.Attach(slot);
    m_db.ObjectStateManager.ChangeObjectState(slot, EntityState.Modified);
    m_db.SaveChanges();       
}

Same problem, exception thrown, and it doesn't make sense to do that anyway.

I have checked the value of the Concurrency field just before saving and it is the same as that in the database.

I have even toyed with saving the Concurrency value, converting it to a Base 64 string, then converting back to a byte array

string c = Convert.ToBase64String(slot.Concurrency);
slot.Concurrency = Convert.FromBase64String(c);

Am I missing something? Is there another call I should be making?

Apart from this behaviour, the optimistic concurrency is working as expected when fetching from the database and viewing the item detached, then saving.

Note that I am not including my Try/Catch code.

Update

The following works as expected:

m_db.ExecuteStoreCommand("UPDATE AppointmentSlot SET ReturnAppointmentSlotID = {0} WHERE AppointmentSlotID = {1} AND Concurrency = {2}", returnAppointmentSlotID, firstAppointmentSlotID, slot.Concurrency);

Update 20131206

Confusingly I've added another method that uses the same approach (fetch - update some fields - save back) which does not cause the exception, so the problem must lie higher up. Perhaps some reference are being held.

1 Answer 1

0

For future reference I've figured it out. I'm using the one context per request approach. It would appear that there must be a reference being held somewhere in the context. If I change my code to the following then it works as expected.

using(AppEntities ae = new AppEntities()){
    var slot = ae.AppointmentSlots.Single(m => m.AppointmentSlotID == firstAppointmentSlotID);
    slot.ReturnAppointmentSlotID = returnAppointmentSlotID;
    ae.ObjectStateManager.ChangeObjectState(slot, EntityState.Modified);
    ae.SaveChanges();
}

The problem probably lies in the fact that I'm performing actions on related entities. (An appointment has a patient which has appointments each of which have a patient etc circular reference)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.