I have a property Statethat, during installation, updates the entity in the database.
The problem is that this property is set through several threads , and sometimes the task field is simultaneously bound to two contexts, which leads to the following exception:
An entity object cannot reference multiple instances of IEntityChangeTracker.
I tried using a lock around the statement using, but this does not work:
private Job job;
public string State
{
get
{
return job.State;
}
set
{
lock (job)
{
using (MyEntities context = new MyEntities())
{
context.Jobs.Attach(job);
job.State = value;
context.SaveChanges();
}
}
}
}
What is the best way to do this?
source
share