Sqlexception from Entity Framework - New Transaction Is Not Allowed Because There Are Other Threads Running in the Session

SqlException from Entity Framework - New transaction is not allowed because there are other threads running in the session

After much pulling out of hair I discovered that the foreach loops were the culprits. What needs to happen is to call EF but return it into an IList<T> of that target type then loop on the IList<T>.

Example:

IList<Client> clientList = from a in _dbFeed.Client.Include("Auto") select a;
foreach (RivWorks.Model.NegotiationAutos.Client client in clientList)
{
var companyFeedDetailList = from a in _dbRiv.AutoNegotiationDetails where a.ClientID == client.ClientID select a;
// ...
}

Entity Framework new transaction is not allowed because there are other threads running in the session, multi thread save

You should create a context for each transaction and then dispose it, you can do that like this:

using(var ctx = new MyContext()) {
//do transaction here
}

After the closed bracket the context is disposed.

For better understanding refer to this post where you can find a great answer by ken2k. Hope you can fix you issue :)

UPDATE:

You should also try adding .ToList() to every LINQ query you have. When you iterate over a LINQ result, you can't make any changes until the iteration has finished. Check if you have something like that or share more code i.e. the piece of code where you call WriteTrace. Hope that this time this actually helps you.

Entity Framework - New transaction is not allowed because there are other threads running in the session

This might be because you are enumerating results when trying to save changes.

Try changing this line:

var list = statusRepository.AsQueryable()
.Where(x => x.DoseReturnID == oldDoseReturn.DoseReturnID);

to:

var list = statusRepository.AsQueryable()
.Where(x => x.DoseReturnID == oldDoseReturn.DoseReturnID)
.ToList();

As a side note invoking .SaveChanges() inside a loop is usually not a good idea as it is in general an expensive operation (talks to the database).

EntityFramework Exception : New transaction is not allowed because there are other threads running in the session

Look at this .
This should solve your issues if its similar.

Entity Framework Exception

Error: System.Data.SqlClient.SqlException: New transaction is not allowed because there are other threads running in the session

Ohhhh. I think you are re-using your Entities wayyyy too much. Try initializing a new context for each transaction you use... Do this (Again, remove the SaveChanges from the UpdateProduct function so that you will only call SaveChanges once...Try this before your if-else-if block

using(var context = new CommonLayer.DBModelEntities()){
if (od.ProductQuantity > orderDetails.ProductQuantity)
{
product.ProductQuantity -= (od.ProductQuantity - orderDetails.ProductQuantity);
daprod.UpdateProduct(product, context);
context.Entry(orderDetails).CurrentValues.SetValues(od);
}
else if (od.ProductQuantity < orderDetails.ProductQuantity)
{
product.ProductQuantity += (orderDetails.ProductQuantity - od.ProductQuantity);
daprod.UpdateProduct(product, context);
context.Entry(orderDetails).CurrentValues.SetValues(od);
}

context.SaveChanges();
}

With this, you should also change the UpdateProduct function to receive the current database context you are using

public void UpdateProduct(CommonLayer.PRODUCT product, CommonLayer.DBModelEntities context)
{
CommonLayer.PRODUCT ExistingProduct = this.GetProduct(product.ProductId);
context.Entry(ExistingProduct).CurrentValues.SetValues(product);
}

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

The pp variable isn't a collection of objects, it's an enumerator that can return objects. While you use the enumerator, the source has to remain open.

Use the ToList method to realise the enumerator into a collection. That will read all items from the enumerator and close the connection to the source, so that you can use the connection for other things.

foreach (var p in pp.ToList())

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

Your Where() statement doesn't retrieve the data; just "opens the cursor" (in old-speak). So, you can't do SaveChange(). The simplest solution is to convert IEnumerable to List or Array:

var rootCategories = _dbContext.SourceCategory.Where(c => c.ParentCategoryId == parentId).ToList();

But I would strongly recommend you google the error and understand why it is happening. To do this recursively is begging for trouble

New transaction is not allowed because there are other threads running in the session. Entity Framework

Save after iterations like below

using (var _sdb = new MyContext())
{
foreach (var myvar in _sdb.Vars)
{
// Change myvar
}
//save at the end
_sdb.SaveChanges();
}

db.SaveChanges in ForEach causes 'New transaction is not allowed because there are other threads running in the session'

You have a bug in your code. You declared and created L outside of the loop. Each time you add the same L , only with different data. In the end you have list of the same data that was created during the last foreach loop cicle.

try this:

        foreach (var item in db.TempLeaves)
{
var z = int.Parse(item.LT) - 1;
var L = new Leave{
Pcode = Int32.Parse(item.Cod),
LeaveTimeStart = StringToHour(item.LeaveTimeStart),
LeaveTimeEnd = StringToHour(item.LeaveTimeEnd),
LeaveDays = int.Parse(item.LeaveDays),
LT = z == 0? Leave.LeaveType.Saati : Leave.LeaveType.Roozane
};
db.Leaves.Add(L);
}

or this

  var leaves= new List<Leave>();
foreach (var item in db.TempLeaves)
{
var z = int.Parse(item.LT) - 1;
var L = new Leave{
Pcode = Int32.Parse(item.Cod),
LeaveTimeStart = StringToHour(item.LeaveTimeStart),
LeaveTimeEnd = StringToHour(item.LeaveTimeEnd),
LeaveDays = int.Parse(item.LeaveDays),
LT = z == 0? Leave.LeaveType.Saati : Leave.LeaveType.Roozane
};
leaves.Add(L);
}
if(leaves.Count>0)
{
db.Leaves.AddRange(leaves);
db.SaveChanges();
}

if you want to use async save you have to make async action at first.



Related Topics



Leave a reply



Submit