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())
A way to get around this is to call .ToList()
on your collection before iterating it.
And while you're at it, call context.SaveChanges()
only once after the loop exits to speed up the code.
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