aspnet/EntityFramework — pull request #946 by ajcvickers (2014-10-23 20:43:22+00:00)
Title: Allow data store selection to happen in OnConfiguring (Issue #932)

Or: Attack of the Semi-Deep-Memberwise-Copies

Previously the common scenario for DbContextOptions was that a new object would be created for each context instance. This allowed it to be mutated in OnConfiguring such that, for example, a different data store could be selected each time. There was also the case where DbContextOptions was put into DI or passed to the constructor, in which case OnConfiguring would be called once and the options would be locked.

The recent changes to options means that the common case is now for DbContextOptions to be in DI. This means that this single object is shared by all context instance created by the app. This results in two problems:
* The locking now happens almost always, effectively making it impossible to dynamically select options in OnConfiguring
* The dynamic selection is not thread safe, but it can be executed by multuple threads all sharing the single object.

The solution is to clone DbContextOptions before passing it to OnConfiguring. This means that all context instance can share common options and also can dynamically change those options in OnConfiguring without impacting other instances. This restores the previous functionality, removes the thread safety issue in the normal path, and also means that locking is no longer required.