Avatar ·

SqlException: New transaction is not allowed because there are other threads running in the session (Entity Framework)

Hello everyone! I'm new to C# and Entity Framework, and I've encountered a confusing error. I'm writing a simple ASP.NET MVC application with SQL Server. There is a method in the service that should update several records in the database. I'm using a `foreach` loop, and inside the loop I call `SaveChanges()` for each record. As a result, I get an exception: **SqlException: New transaction is not allowed because there are other threads running in the session.** Here's the simplified code: ```csharp public void UpdateOrders(List orders) { using (var context = new MyDbContext()) { foreach (var order in orders) { order.Status = "Processed"; context.Entry(order).State = EntityState.Modified; context.SaveChanges(); // Error here on the second iteration ```