2

I have an application that implements entity framework 4.1, i use a context instance for every method

object select()
{
    using(DBContext context = new DBContext())
    {
        return context.table.Where(e => e.id == 1).FirstOrDefault();
    }
}

void update(entity)
{
    using(DBContext context = new DBContext())
    {
        context.Attach(entity);
        context.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
        context.SaveChanges();
    }
}

it work fine, the problem is that the application es multi-user and they can modify the same records, i want to prevent that one user change a record that is being modifying by other user until update the record

entity = select(); // get the record and lock it
// do some change ,nobody can modify this record in the db
update(); // update the record and unlock it

is there some way to achieve this?

Chuy
  • 53
  • 2
  • 6
  • 3
    Don't do this - use the "optimistic concurrency" model instead: you grab the entity, edit it, and when you save it, you make sure it hasn't been changed in the meantime. Much more efficient and ultimately much easier to work with, less hassle and less potential for conflicts. – marc_s Jan 16 '14 at 21:25
  • Hear hear. See this: http://msdn.microsoft.com/en-us/data/jj592904.aspx – Gert Arnold Jan 16 '14 at 21:34
  • i see, thanks for the info @marc_s; do you know how to implement optimistic concurrency in entity framework with detached entities? – Chuy Jan 16 '14 at 21:38

0 Answers0