Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it possible to get Entity Framework to recognize previous migrations if you change your project namespace?
    primarykey
    data
    text
    <p>Let me break down the scenario:</p> <ol> <li><p>I create my models/mappings using the code-first approach</p></li> <li><p>I setup a database initializer for <code>MigrateDatabaseToLatestVersion</code></p></li> <li><p>I create a migration using <code>add-migration</code></p></li> </ol> <p>This creates a <code>Configuration</code> class like so:</p> <pre><code>namespace MyApp.Migrations { internal sealed class ConfigurationInfo : DbMigrationsConfiguration&lt;MyContext&gt; { } } </code></pre> <p>I can run my code and the database will be automatically created with no issue.</p> <p>Now I go in and change the namespace that my <code>Configuration</code> class lives under:</p> <pre><code>namespace MyApp.Data.Migrations // &lt;-- new namespace { internal sealed class ConfigurationInfo : DbMigrationsConfiguration&lt;MyContext&gt; { } } </code></pre> <p>I drop the database and rerun the code. I now get this message:</p> <blockquote> <p>Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.</p> </blockquote> <p>When I renamed the namespace that <code>Configuration</code> lived under it no longer recognizes any of the migrations that were previously created.</p> <p>I did a lot of experimenting and when I set <code>MigrationsNamespace</code> equal to the old value in the <code>Configuration</code> constructor like so:</p> <pre><code> namespace MyApp.Data.Migrations { internal sealed class ConfigurationInfo : DbMigrationsConfiguration&lt;MyContext&gt; { public ConfigurationInfo() { AutomaticMigrationsEnabled = false; MigrationsNamespace = "MyApp.Migrations"; // &lt;-- this works } } } </code></pre> <p>Now everything works, except all of the previously created migrations need to live under the old namespace in order to work, as well as all future ones (which get the old namespace automatically).</p> <p>This workaround didn't really do what I wanted to do, which is be able to refactor my code and still have entity framework recognize my previous migrations.</p> <p>What if the name of my project changes, but I have multiple installations out there that are depending on the MigrateDatabaseToLatestVersion database initializer to receive schema changes to my code?</p> <p>Am I locked into using the same namespace for my DAL as soon as I enable migrations?</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. 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