Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's the approach I eventually used, using the custom <code>IDatabaseInitializer&lt;T&gt;</code> code from <a href="https://stackoverflow.com/a/16163466/43140">this answer</a>, which helped me out a great deal.</p> <p>First we add another constructor to the <code>DataStore</code> class (<code>DbContext</code>) which doesn't require the connection string parameter:</p> <pre><code>public class DataStore : DbContext, IDataStore { public int UserID { get; private set; } // This is the constructor that will be called by the factory class // if it is initialised without a connection string parameter public DataStore(int userId) { UserID = userId; } public DataStore(int userId, string connectionString) : base(connectionString) { UserID = userId; } public virtual IDbSet&lt;User&gt; Users { get; set; } // Rest of code here } </code></pre> <p>Then we do the same for the factory class:</p> <pre><code>public class DataStoreFactory : Disposable, IDataStoreFactory { private DataStore _database; private int _userId; private string _connectionString; // This is the constructor that will be called by the // MigrationDataStoreFactory class public DataStoreFactory(int userId) { _userId = userId; } public DataStoreFactory(int userId, string connectionString) { _userId = userId; _connectionString = connectionString; } public IDataStore Get() { // If we have a connection string, construct our context with it, // if not, use the new constructor if(_connectionString != null) _database = new DataStore(_userId, _dateTimeServices, _connectionString); else _database = new DataStore(_userId, _dateTimeServices); return _database; } protected override void DisposeCore() { if (_database != null) _database.Dispose(); } } </code></pre> <p>This is the custom initializer code:</p> <pre><code>public class MigrateDatabaseToLatestVersionWithConnectionString&lt;TContext, TMigrationsConfiguration&gt; : IDatabaseInitializer&lt;TContext&gt; where TContext : DbContext where TMigrationsConfiguration : DbMigrationsConfiguration&lt;TContext&gt;, new() { private readonly DbMigrationsConfiguration _config; public MigrateDatabaseToLatestVersionWithConnectionString() { _config = new TMigrationsConfiguration(); } public MigrateDatabaseToLatestVersionWithConnectionString(string connectionString) { // Set the TargetDatabase for migrations to use the supplied connection string _config = new TMigrationsConfiguration { TargetDatabase = new DbConnectionInfo(connectionString, "System.Data.SqlClient") }; } public void InitializeDatabase(TContext context) { // Update the migrator with the config containing the right connection string DbMigrator dbMigrator = new DbMigrator(_config); dbMigrator.Update(); } } </code></pre> <p>Our custom context factory (which is only ever called by Code First Migrations) can now carry on using the <code>DataStore</code> constructor which <em>doesn't</em> require a connection string:</p> <pre><code>public class MigrationDataStoreFactory : IDbContextFactory&lt;DataStore&gt; { public DataStore Create() { return new DataStore(0); } } </code></pre> <p>As long as we set the database initializer to our custom initializer and pass in the connection string (which in my case is done in <code>Global.asax</code>), migrations will use the correct connection:</p> <pre><code>Database.SetInitializer&lt;DataStore&gt;(new MigrateDatabaseToLatestVersionWithConnectionString&lt;DataStore, MyMigrationsConfiguration&gt;(INJECTED_CONNECTION_STRING_HERE)); </code></pre> <p>Hope all that makes sense—feel free to ask for clarification in the comments. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload