none
Is ContextOptions.LazyLoadingEnabled thread safe? RRS feed

  • Question

  • It appears to me that the LazyLoadingEnabled feature is a global setting.

    However, what happens if I want to control the entity object tree individually, in a more fine granular manner?

    What if, in a multi-threaded application, one thread sets LazyLoadingEnabled to true while in the other thread LazyLoadingEnabled is simultaneously set to false?


    Still people out there alive using the keyboard?

    Working with SQL Server/Visual Studio/Office/Windows and their poor keyboard support they seem extinct...

    Tuesday, November 11, 2014 5:01 PM

Answers

  • It is a per-context setting. If two threads set the LazyLoadingEnabled property of the same context instance, the last thread will win. It is as simple as that. If you don't want this to happen in your application you need to implement the "thread-safety" yourself, i.e. your code needs to make sure that the property doesn't get set more than once if that's your requirement. How to do that is not an Entity Framework question though.

    Also, the LazyLoadingEnabled property is used to control whether related objects are loaded automatically when a navigation property is accessed. This means that you can safely switch it on and off for a context without having to worry anything about how the data gets persisted.

    Please remember to mark helpful posts as answer and/or helpful.

    • Proposed as answer by Fred BaoModerator Wednesday, November 12, 2014 2:38 AM
    • Marked as answer by BetterToday Wednesday, November 12, 2014 3:17 AM
    Tuesday, November 11, 2014 5:11 PM

All replies

  • It is a per-context setting. If two threads set the LazyLoadingEnabled property of the same context instance, the last thread will win. It is as simple as that. If you don't want this to happen in your application you need to implement the "thread-safety" yourself, i.e. your code needs to make sure that the property doesn't get set more than once if that's your requirement. How to do that is not an Entity Framework question though.

    Also, the LazyLoadingEnabled property is used to control whether related objects are loaded automatically when a navigation property is accessed. This means that you can safely switch it on and off for a context without having to worry anything about how the data gets persisted.

    Please remember to mark helpful posts as answer and/or helpful.

    • Proposed as answer by Fred BaoModerator Wednesday, November 12, 2014 2:38 AM
    • Marked as answer by BetterToday Wednesday, November 12, 2014 3:17 AM
    Tuesday, November 11, 2014 5:11 PM
  • Thank you for your valuable answer.

    OK, so - as far as I understand from your reply - the only option to locally control lazy loading (and, thus, triggering or preventing database access whenever calling NavigationProperty.Count()) is to set LazyLoadingEnabled to false and call NavigationProperty.Load() whenever appropriate. Right?


    Still people out there alive using the keyboard?

    Working with SQL Server/Visual Studio/Office/Windows and their poor keyboard support they seem extinct...

    Wednesday, November 12, 2014 3:23 AM