2

Trying to execute an insert stored procedure in a loop using Entity Framework, something like this:

var context = new Entities()
var items = context.GetAllItems().ToList();

foreach(var item in items) 
{
    // Other select and update procedures
    context.InsertProcedure(item.Prop1);
}

context.SaveChanges();

but I am getting an exception.

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

Anybody had this before?

2
  • 2
    This may happen if you have a select statement within your store procedure... do you have something like that?... if this is not the case there are 2 other options: Why do you need an 'Insert' sp if you are already using EF to map entities? Option 2. Have you considered the use of a TransactionScope ? Commented Dec 5, 2016 at 22:39
  • @DavidEspino There is a SELECT @@identity statement, I have to use the SP, its a reusable part used elsewhere, what is the transaction scope ? Commented Dec 5, 2016 at 22:55

1 Answer 1

1

It turned out there is another exception, but looks like this exception is always thrown when there is a problem within the loop.

What I did is that I changed the return type of the stored procedure to None in Model Browser

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.