Using EF5 database first. On creation of my context, I'm setting ContextOptions.LazyLoadingEnabled
to true
. Something happens to change that to false
(nowhere in our code is that setting changed).
At various points, I'm checking whether it's still true
by using this snippet of code (getting context from an entity object). The problem only manifests when running multiple threads (load some entities, then pass them through multi-threaded code). It doesn't occur at any consistent point in the code, so it's impossible to determine what exactly causes it.
I guess I'm just hoping somebody might know more about the internal workings of EF and give me a clue...
An Entity Framework context is not thread safe. The problem you encounter occurs when multiple threads access a property that is lazy loaded. Internally EF switches off lazy loading when checking the value of a NavigationProperty and switches it back on afterwards. (See RelatedEnd.DisableLazyLoading()
)
Thus what is happening in your case probably goes like this: One thread is currently accessing a NavigationProperty and before it finishes another thread tries to access a navigation property of an object attached to the same context. At that point in time lazy loading is switched off temporarily and thus no data is loaded.
EF explicitly states that the ObjectContext is not thread safe, so it is probably a good idea to make sure no concurrent access is happening from different threads.
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