The context objects generated by Entity Framework are not thread-safe.
What if I use two separate entity contexts, one for each thread (and call SaveChanges()
on each) - will this be thread-safe?
// this method is called from several threads concurrently
public void IncrementProperty()
{
var context = new MyEntities();
context.SomeObject.SomeIntProperty++;
context.SaveChanges();
}
I believe entity framework context implements some sort of 'counter' variable which keeps track of whether the current values in the context are fresh or not.
- With the code above - called from separate threads - do I still need to lock around the increment/savechanges?
- If so, what is the preferred way to accomplish this in this simple scenario?