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?
sp_prefix for your stored procedures. Microsoft has reserved that prefix for its own use (see Naming Stored Procedures), and you do run the risk of a name clash sometime in the future. It's also bad for your stored procedure performance. It's best to just simply avoidsp_and use something else as a prefix - or no prefix at all!ToList()to force it to load all data?