I am currently getting this error in an EF method that is being called (having got Elmah workking)
New transaction is not allowed because there are other threads running in the session.
I have looked at this :
and similar questions, but these all refer to savechanges being called within a foreach
loop. My code doesn't have a foreach
loop, so i'm struggling to find the issue.
Controller (inherited from ApiController)
static readonly IMyRepository myRepository = new MyRepository();
public HttpResponseMessage PutObject(int id, int id2)
{
if(!myRepository.Update(id,id2))
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound);
}
else
{
return Request.CreateResponse(HttpStatusCode.OK);
}
}
Repository
MyEntities _myEntities;
public MyRepository(MyEntities context)
{
_myEntities = context;
}
public MyRepository()
{
}
public bool Update(int id,int id2, string id3, int id4)
{
_myEntities = _myEntities ?? new MyEntities();
//update by id if id2,id3 and id4 are zero
if (id2 == 0 && id3 == "0" && id4 == 0)
{
var myobject = _myEntities.MyObjects.Where(x => x.id == id);
if (myobject.Count() > 0)
{
MyObject temp = myobject.SingleOrDefault();
temp.Processed = true;
_myEntities.SaveChanges();
return true;
}
else
{
return false;
}
}
else
{
var myobject = _myEntities.MyObjects.Where(x => x.SourceID == id && x.ExternalID == id2 && x.InternalID == id3 && x.Code == id4 && x.Processed == false);
if (myobject.Count() > 0)
{
myobject temp = myobject.SingleOrDefault();
temp.Processed = true;
_myEntities.SaveChanges();
return true;
}
else
{
return false;
}
}
}
is this because the IQueryable returned by the linq, still holds an open connection?