Context:
I'm doing a migration from Access to SQL Server. It's a complex'ish migration and ran fairly often -- so I integrated it into the website so the user can do it on their own occasionally, if needed.
Weirdness:
So I initially decided to go with one context doing regular SaveChanges()
at the end of each area.
When it got to ~ 60k inserts, it took a VERY long time to save.
Ok, I thought, I'll just run SaveChanges
after each entry. It took a long time. Obscenely long.
Ok, so I decided to try SaveChangesAsync
and simply couldn't get it to work. I was willing to give this one area it's own Dbcontext
and just let it save in the background and before the finale I would await it. Couldn't get that to work. Initially it'd throw an error and then it simply wouldn't save the data.
Ok, so the next step was to try giving this method its own DbContext
. Worked like a DREAM.
I then gave each method (about 13 in all) their own DbContext
. Everything was very fast.
So my question is: what's the difference between one DbContext
, with 13 SaveChanges()
calls, and 13 DbContexts
with 13 calls to SaveChanges()
?
Is there something left behind after you run SaveChanges()
that I could clean? Or should I simply just go with many DbContext
's? I was simply trying to avoid opening a new connection each and every time to shave off that little bit of time but it's not really a big deal but I'm still lacking in understanding why this is the case. Can someone educate me?