[c#] New transaction is not allowed because there are other threads running in the session LINQ To Entity


What's happening is that you're using one SQL connection to iterate over a collection of db entities, then using another connection to save changes. This happens because your classes are basically "married" to one instance of your db connection and cannot be changed by another.

A way to get around this is to call .ToList() on your collection before iterating it.

And while you're at it, call context.SaveChages() only once after the loop exits to speed up the code.


Any ideas on why this could be breaking?

foreach (var p in pp)
    ProjectFiles projectFile = (ProjectFiles)p;
    projectFile.Status = Constants.ProjectFiles_ERROR;
    projectFile.DateLastUpdated = DateTime.Now;

I read that the workaround the issue, is to retrieve the results in one go before the foreach loop.

But didnt I do that? "pp" is the collection of results in my case