Note that there are some explanatory texts on larger screens.

plurals
  1. POEntity Framework CodeFirst Migrations, Running migrations with optional upgrading to newest changes
    text
    copied!<p>We are running <strong>Entity Framework 5</strong> with <strong>Code First</strong> and <strong>Migrations Enabled</strong>. Running with the database initializer: <code>MigrateToLatestVersion</code> </p> <p>We have several customers live that run on the latest stable branch. While we develop new code in our trunk/master, we sometimes have the need to connect to the customers database running on branched code, fx. to debug some sort of data bug.</p> <p>This can be "dangerous" if someone forgot to switch to the branch the customer is running, because migrations will then upgrade the customers database to fit whatever code that person is running.</p> <p>One solution would be to run another initializer that doesnt migrate to the latest version. But that would mean a lot more work when we deploy new systems to new customers or someone new joins the team and needs go get up and running.</p> <p>We were thinking of solving this problem by having a bool in the app.config to set if the code should "Migrate To Latest Version" and then always have that to false for development, and then transforming it when deploying to customers.</p> <p>That way we get the benefit of still having the database updating to the newest version automaticly, but not the danger of a developer accidentally connecting to a system of an older version of the code, and migrations destroying that database.</p> <p>So now that we have that, we basicly need to check this:</p> <p>(simplified code)</p> <pre><code>if(Config.MigrateToLatestVersion || !databaseExists) { var initializer = new MigrateToLatestVersion&lt;MyContext,MigrationConfiguration&gt;(); Database.SetInitializer(initializer); using(var context = new MyContext()) { context.Database.Initialize(true) } } </code></pre> <p>My question was going to be about how to check if the database exists without running migrations, but i found out you can do this:</p> <pre><code>Database.SetInitializer&lt;MyContext&gt;(null); var context = new MyContext(); var databaseExists = context.Database.Exists(); </code></pre> <p>But if i only run the <code>MigrateToLatestVersion</code> initializer when the database doesnt exist or when i manually run Update-Database from the package-manager console.</p> <p>I have 2 problems, first: I no longer get an exception saying if my model is different from the database, which i would still like. second: it doesnt run the seed method located in my MigrationConfiguration, which i might still want to run.</p> <p>Any suggestions on how i can still run the Migrations Initializer to get all the benefits, but still have something that prevents someone from potentially breaking our production environments by accident?</p>
 

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