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; }