Entity Framework 4.3.1 migrations always call the default constructor and ignore the connection string

I want to perform (code) migrations using connectionString, which I created myself.

The entry point is a static function with one parameter, connectionString.

I have a DbContext class called PocModel with a default constructor.

public PocModel() : base("PocModel") { ... 

And a context constructor with a string argument:

 public PocModel(string nameOrConnectionString): base(nameOrConnectionString) { ... 

My goal is to migrate to a database that targets connectionString, rather than connectionString, which EF magically creates using only the name (localhost \ sqlexpress - PocModel).

That I could not do.

I have an "external" function declared as follows:

 public static void MigrateDatabase(string connectionString) { ... 

This function that I tried to implement in flw. ways:

  DbMigrator migrator = new DbMigrator(new Migrations.Configuration()); migrator.Configuration.TargetDatabase = new DbConnectionInfo(connectionString, "System.Data.SqlClient"); migrator.Update(); 

And like this:

 Database.DefaultConnectionFactory = new SqlConnectionFactory(connectionString); Database.SetInitializer<PocModel>(new MigrationInitializer()); PocModel model = new PocModel(connectionString); model.Dispose(); 

I even tried to dynamically install

 ConfigurationManager.ConnectionString["PocModel"] 

To the connection transmitted.

But alas, everything fails, and the default PocModel constructor is called from the EF migration code, targeting the server: localhost \ sqlexpress and the database: PocModel.

I was unable to migrate to any database not named "PocModel" located on "localhost \ sqlexpress".

I cannot use the app.config file to set my connectionString as I need to pass this through a static external function.

Please help, stuck in this problem for a very long time.

EDIT: I did this with this hack, but I ask a question to make sure there really is no other solution to the problem (since EF errors are wrong)

  private static string _databaseNameOrConnectionString = "PocModel"; internal static string DatabaseNameOrConnectionString { get { return _databaseNameOrConnectionString; } //HACK: This setter must ONLY be called from SetupModule.MigrateDatabase. It is a hack to circumvent the code-first migrations usage of this ctor. set { _databaseNameOrConnectionString = value; } } //the code first migrations will call this ctor and ignore the connectionString it have been passed. public PocModel() : base(DatabaseNameOrConnectionString) { 
+4
source share
3 answers

The call to Update-database uses the default constructor, so you need to change this function. You mentioned that you have a static class for this, I could not figure out how to make this work without a static helper.

  public static class Helper { public static string GetConnectionString() { SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(); builder.DataSource = @".\sqlexpress"; builder.InitialCatalog = "migrateme"; builder.IntegratedSecurity = true; builder.MultipleActiveResultSets = true; return builder.ConnectionString; } } 

and then instead of calling the base constructor, I called the constructor with a custom string and created the correct database

 public PocModel() : base(Helper.GetConnectionString()) { } 
+1
source

I had the same problem as you. However, I found a solution that you said you tried but did not work. The following code worked for me. I found it here http://romiller.com/2012/02/09/running-scripting-migrations-from-code/

 var configuration = new Configuration(); configuration.TargetDatabase = new DbConnectionInfo("Server=MyServer;Database=MyDatabase;Trusted_Connection=True;", "System.Data.SqlClient"); var migrator = new DbMigrator(configuration); migrator.Update(); 
0
source

For those who consider that their connection string is ignored ...

It seems like a bug in this part of the framework

 var configuration = new TMigrationsConfiguration(); configuration.TargetDatabase = new DbConnectionInfo(context.Database.Connection.ConnectionString,"System.Data.SqlClient"); var migrator = new DbMigrator(configuration); migrator.Update(); 

Doesn't act like

 var configuration = new TMigrationsConfiguration(); var migrator = new DbMigrator(configuration); migrator.Configuration.TargetDatabase = new DbConnectionInfo(context.Database.Connection.ConnectionString,"System.Data.SqlClient"); migrator.Update(); 

That is, the Target database is ignored if it is changed after the migrator is created. You must set the TargetDatabase before adding to migrator.

0
source

All Articles