Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of MigrateDatabaseToLatestVersion useSuppliedContext = false?

Something I ran into recently.

I have a project which dynamically generates connection strings and I'm trying to use MigrateDatabaseToLatestVersion on the context that wraps these. Every time I would do this I would see my dynamic db not be created, but instead the db on my default constructor connection string (used for testing) migrated over and over.

After digging through the EF migrations source code I find that MigrateDatabaseToLatestVersion has a constructor

// Summary:
//     Initializes a new instance of the MigrateDatabaseToLatestVersion class specifying
//     whether to use the connection information from the context that triggered initialization
//     to perform the migration.
//
// Parameters:
//   useSuppliedContext:
//     If set to true the initializer is run using the connection information from the
//     context that triggered initialization. Otherwise, the connection information
//     will be taken from a context constructed using the default constructor or registered
//     factory if applicable.
public MigrateDatabaseToLatestVersion(bool useSuppliedContext);

Not being flippant but what is the reason why you would want to ever migrate the context that is not the one that is being migrated? Why is that the default? Does anyone have any insight into the thinking here?

like image 851
George Mauer Avatar asked Oct 17 '25 10:10

George Mauer


1 Answers

I want to know the answer to this question myself. I do not know why the context was designed that way. However, I can venture a guess as to why the current default is useSuppliedContext=false.

I decompiled the first version of EntityFramework to include migration support, EntityFramework-4.3.0, because I suspect that the default behavior is for backwards compatibility purposes. I looked at the decompiled implementation of IDatabaseInitializer<TContext>.InitializeDatabase(TContext context) in MigrateDatabaseToLatestVersion. Guess what? In EntityFramework-4.3.0, the context parameter of that method is completely ignored. So it can’t possibly respond to explicitly-provided connection parameters/settings because those are only accessible through that context variable.

It looks like support for respecting context was added in EntityFramework-6.1.1. Prior to that, your only option was to pass a connection string to MigrateDatabaseToLatestVersion’s constructor. I think this would have prevented you from using the same DbContext type for different backends in the same process. I bet that the new feature of respecting the context (and behaving correctly, IMO) would not have been accepted into EntityFramework if it was enabled by default because that would change behavior which stable projects may be relying on and otherwise prevent projects from adopting it.

The exact reasoning is actually given as a comment in commit 777a7a77a740c75d1828eb53332ab3d31ebbcfa3 by Rowan Miller:

Also swapping the new useSuppliedContext parameter on MigrateDatabaseToLatestVersion`.cs to be false by default since we are going to be shipping this change in a patch release.

like image 68
binki Avatar answered Oct 19 '25 08:10

binki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!