I have worker role that does some work with the database every second.
Is it ok to initialize the DbContext
when the worker is started and use it throughout the lifetime of the worker?
How is the db connection handled? What if the database goes offline and back online? Will I still be able to use the context?
My advice is to create, use and destroy the context for each operation... do not hang onto it. I used to worry at first because I had no idea how expensive it was to create the DbContext, the answer is, not very.
If you attempt to keep reusing a DbContext instance you will also run into problems (very quickly) as it's internal state models will start reporting conflicts for versions of the object that have been loaded (updated whatever) previously
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