Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So the solution i went with was to make a new DatabaseInitializer called <code>MigrateDatabaseToLatestIfLocal</code></p> <p>Looking how the <a href="http://msdn.microsoft.com/en-us/library/hh829293%28v=vs.103%29.aspx" rel="nofollow">MigrateDatabaseToLatestVersion</a> initializer worked, i did something very similar, with the difference of checking if the database exists and if we were running locally or remote (With the help of config transformations to determine this. The remote system has it's datasource transformed in the config file.)</p> <pre><code>public class MigrateDatabaseToLatestIfLocal&lt;TContext, TMigrationsConfiguration&gt; : IDatabaseInitializer&lt;TContext&gt; where TContext : DbContext where TMigrationsConfiguration : DbMigrationsConfiguration&lt;TContext&gt;, new() { private DbMigrationsConfiguration _config; public MigrateDatabaseToLatestIfLocal() { this._config = (DbMigrationsConfiguration)Activator.CreateInstance&lt;TMigrationsConfiguration&gt;(); } public MigrateDatabaseToLatestIfLocal(string connectionStringName) { MigrateDatabaseToLatestIfLocal&lt;TContext, TMigrationsConfiguration&gt; databaseToLatestVersion = this; var instance = Activator.CreateInstance&lt;TMigrationsConfiguration&gt;(); instance.TargetDatabase = new DbConnectionInfo(connectionStringName); databaseToLatestVersion._config = instance; } public void InitializeDatabase(TContext context) { var databaseExists = context.Database.Exists(); var migrator = new DbMigrator(this._config); var pendingMigrations = migrator.GetPendingMigrations().OrderByDescending(s =&gt; s); var localMigrations = migrator.GetLocalMigrations().OrderByDescending(s =&gt; s); var dbMigrations = migrator.GetDatabaseMigrations().OrderByDescending(s =&gt; s); var isRemoteConnection = FunctionToFindOutIfWeAreRemote(); //here we check the config file to see if the datasource is a certain IP, this differentiates locally and remotely because of config tranformation. if (isRemoteConnection &amp;&amp; databaseExists) { if (pendingMigrations.Any()) { throw new MigrationsException("You are not allowed to automatically update the database remotely.") } if (localMigrations.First() != dbMigrations.First()) { throw new MigrationsException("Migrations in code and database dont match, please make sure you are running the code supported by the remote system. "); } } else { //we are local, fine update the db and run seeding. //we are remote and the db does not exist, fine update and run seed. migrator.Update(); } } } </code></pre> <p>This might be a very special case, but to me it provides some safety when running code first migrations, that make sure you dont just randomly migrate a live environment accidently</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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