title: Migrations in Entity Framework in a collaborative environment
body: <p>We have multiple developers working on a project that uses Entity Framework 5.0. Every developer uses his own local SQL 2012 database so he can develop and test without impeding others.</p>
  
  <p>At first, we used a hybrid of automatic migrations and code-based migrations. That didn't work well at all so we decided to disable automatic migrations and to only allow code-based. I should add that we started again with a clean database without a 'corrupted' <code>_MigrationsHistory</code> from all the automatic migrations.</p>
  
  <p>So now the workflow is:</p>
  
  <ol>
  <li>Developer changes his datamodel</li>
  <li>Does <code>add-migration &lt;Name&gt;</code> and applies it to his database with <code>update-database</code>.</li>
  <li>Checks in the datamodel change and the migration into Git. </li>
  <li>Another developer pulls, receives the changes and applies it to his database.</li>
  </ol>
  
  <p>So far, this worked well. However before today it was usually just me who made the migrations and the others applied them. But today there were migrations from three developers. I just pulled those migrations, did an <code>update-database</code> which went fine. </p>
  
  <p>I also had a change to my own datamodel however so at the end of the <code>update-database</code> it gave me a warning that I still wasn't up to date so I did <code>add-migration &lt;my migration&gt;</code>. However when it scaffolded the migration, it gave me the changes of all the migrations I had already applied to the database. So: it tried to drop columns that had already been dropped, tried to create a table that already existed, etc. </p>
  
  <p>How can that be? My assumption was that EF would just check the <code>_MigrationsHistory</code> table and find out which migrations weren't present in the table yet and apply those one by one ordered by the timestamp that's part of the name. But apparently not, because even when I undo my own changes and I have a clean environment it still complains my database isn't in sync with the model. But I just pulled those changes and applied them to my database. It <em>is</em> in sync. I can see the migrations that I just applied in the <code>_MigrationsHistory</code> table too.</p>
  
  <p>The only thing I can think of is that I added a property to a datamodel that wouldn't result in a database change (I added a <code>List&lt;X&gt;</code> to datamodel Y where X is the many in the one-to-many relationship. This wouldn't result in a database change as X already had a foreign key to Y). Could that be it? If so, that's really fragile because there's no way to add a migration for that since there's no database change and I'm not sure how to fix this either.</p>
  
  <p>I'm not sure how to deal with this, because I can of course just edit what it scaffolded and remove everything that has already been applied to my database. But then what? I check it in and then some other developer gets the same message that his database isn't up to date even after applying my new changes, scaffolds his own changes, gets the same nonsense scaffolding, edits it, checks it in and then the <em>next</em> developer gets it. It becomes a vicious circle and a similar one to what we had when we used automatic migrations and I thought we had fixed that by switching to code-based only. I can't trust it right now to do the right thing and it's a nightmare to work with like this.</p>
  
  <p>What I also tried is adding the migrations I pulled from my coworkers one by one with <code>update-database -t:201211091112102_&lt;migrationname&gt;</code> but to no avail. It still gives me the erroneous scaffold.</p>
  
  <p>So what did we do wrong here, or is EF simply not built for collaboration like this? </p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I created a reproducible test case, it's a bit of a lengthy dance though in order to simulate this multi user/multi database scenario.</p>
  
  <p><a href="https://github.com/JulianR/EfMigrationsTest/" rel="noreferrer">https://github.com/JulianR/EfMigrationsTest/</a></p>
  
  <p>Steps to reproduce when you have the above project (these steps are also present in the code):</p>
  
  <ol>
  <li>add-migration Init</li>
  <li>update-database (on database 'TestDb')</li>
  <li>Change connection string to point to TestDb1</li>
  <li>update-database on TestDb1</li>
  <li>Uncomment property Foo on class Test</li>
  <li>add-migration M1 to add property Foo to TestDb1</li>
  <li>Comment out Test.Foo again</li>
  <li>Change connection string to point to TestDb2</li>
  <li>Exclude migration M1 from project so it doesn't get applied to TestDb2</li>
  <li>Uncomment property Bar on class Test</li>
  <li>update-database to apply Init migration to TestDb2</li>
  <li>add-migration M2 to add property Bar to TestDb2</li>
  <li>Change connection string to point to the original TestDb again</li>
  <li>Include migration M1 into the project again</li>
  <li>Uncomment property Foo on class Test</li>
  <li>Uncomment property SomeInt on class Test</li>
  <li>update-database</li>
  <li>add-migration M3</li>
  <li>update-database, get an error because M3 tries to add column Foo to database TestDb which was already just added by migration M1.</li>
  </ol>
  
  <p>The above is to simulate three users, where user 1 inits his database, the other two use his initialization to create their database as well. Then user 2 and user 3 both make their own change to the datamodel and add it to source control together with the migrations needed to apply the changes. Then user 1 pulls the changes of user 2 and 3 while user 1 has also made a change to the database himself. Then user 1 calls <code>update-database</code> to apply the changes of user 2 and 3. He then scaffolds his own migration which then erroneously adds a change from user 2 or 3 to the scaffolded migration which causes an error when applied to user 1's database.</p>