Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Ran Enable-Migrations. Had a bit of trouble due to connection strings and which app.config was getting used, but eventually got it. However, this MSDN page says that this should have automatically generated the first migration to get me to the point I'm already at. It didn't.</p> <p>Ran Add-Migration InitialSchema to accomplish what wasn't automatically done in step 1. This worked.</p> </blockquote> <p>In fact the <code>enable-migrations</code> command creates an initial migration only if your database has already been created with Code-First before in which case the database contains a <code>__MigrationHistory</code> table. If this table does not exist (which is the case when you have an existing database that never has been created before with Code-First) <code>enable-migrations</code> only creates the <code>Configuration</code> class. You have to call <code>add-migration</code> manually then to create the first migration class. So, the behaviour you have seen is expected.</p> <p>Generally the procedure to prepare an existing database for migrations is the following if you are using EF 5:</p> <ul> <li><p>Call <code>enable-migrations</code> in package manager console. A <code>Migrations</code> folder in your project and a <code>Configuration</code> class will be created.</p></li> <li><p>Open the <code>Configuration</code> class and set <code>AutomaticMigrationsEnabled = false</code> in the constructor (if it isn't already by default).</p></li> <li><p>In package manager console call </p> <pre><code>add-migration -IgnoreChanges InitialSchema </code></pre> <p>"InitialSchema" is only an example name. You can name it like you want. A <code>&lt;Timestamp&gt;_InitialSchema</code> class will be created that derives from <code>DbMigration</code>. The <code>Up</code> and <code>Down</code> methods in this class are empty due to the <code>-IgnoreChanges</code> flag. Without this flag the class would contain a migration to add your whole model to the database which is not what you want since the existing database already contains the database schema.</p></li> <li><p>Run <code>update-database</code> in the package manager console. Because the <code>Up</code> method is empty this update does nothing with your existing schema <em>except</em> it creates the <code>__MigrationHistory</code> table (as a system table in your database) and adds the first record to this table that contains a model hash of your current EF model.</p></li> <li><p>Optional last step: If you prefer to work with automatic migrations open the <code>Configuration</code> class and set <code>AutomaticMigrationsEnabled = true</code> in the constructor. If you want to proceed with code-based migrations leave the flag <code>false</code>.</p></li> </ul> <p>At this point you can start to make changes to your model. Everytime you create a new migration with <code>add-migration</code> it will be based on your model before the modification and the migration class will only contain the necessary schema changes.</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