2

我有一个实现实体框架 4.1 的应用程序,我为每个方法使用一个上下文实例

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();
    }
}

它工作正常,问题是应用程序是多用户并且他们可以修改相同的记录,我想防止一个用户更改其他用户正在修改的记录,直到更新记录

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

有什么方法可以做到这一点吗?

4

0 回答 0