I am trying to loop through a list and execute a stored procedure on every item in the list.
foreach(value in values) {
Context.Database
.ExecuteSqlCommand(
"sp_ProcedureName @value1, @value2, @value3",
new SqlParameter("@value1", value.value1.ToString()),
new SqlParameter("@value2", value.value2.ToString()),
new SqlParameter("@value3", value.value3.ToString()));
}
I keep getting the following error:
New transaction is not allowed because there are other threads running in the session.
I tried to use the async method as well. But that didn't seem to work. Is there something I am missing. Or is it not possible to run a stored procedure inside a loop?
Perhaps the db call to populate values has opened a transaction, stopping the inside call functioning.
Try using .ToList() in the call that populates values to preload all of the values first.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With