Getting \"new transaction is not allowed because there are other threads running in the session\".
It has nothing to do with foreach loops or anything people usually
You don't dispose the context when your request is finished. You can dispose the context either by applying a using block ...
using (var context = new MyContext())
{
// Do Db stuff and SaveChanges, etc.
}
// context gets disposed automatically here
... or explicitly:
context.Dispose();
(That's my theory based on your input so far.)