title: Entity Framework Core - Earlier data migrations break when models are changed
body: <p>We are starting a new application with with .NET Core, EF Core and Postgres - we're really liking the migrations :-)</p>
  <p>However, I've found a problem that I can't figure out how to work around. In some of our migrations, we use the <code>DbContext</code> to load records, run some business logic, and resave the records. We also run <code>database.Context.Migrate()</code> when the web server starts, so it always runs.</p>
  <p>This all works fine (so far).</p>
  <p>Unfortunately it works right up until the model is changed at a later point. Consider the following migration order:</p>
  <ol>
  <li><p>Create <code>Customer</code>, <code>Order</code>, and <code>OrderLine</code> models. Add the schema migrations to load the structures</p>
  </li>
  <li><p>A business rule changes, so we need to set a value on the <code>Order</code> record based on some business logic . We create a data migration where we load the <code>DbContext</code>, run the code, and run <code>SaveChangesAsync()</code></p>
  <p>At this point, everything is fine.</p>
  </li>
  <li><p>We need to add a schema migration with a new field to the <code>Customer</code> record.</p>
  </li>
  </ol>
  <p>Here, things break. If the database has not had migration 2 applied, the <code>Migrate()</code> command errors as when applying migration 2, EF is trying to generate a select statement that includes the new field in 3.</p>
  <p>I'm not sure where the problem actually is in the process though: should we not be using the <code>DbContext</code> in the migration (and hand-code SQL statements)? Should we be explicitly be doing projections on every record read so that we know exactly what we're doing?</p>
  <p>Something else?</p>