title: EF5 Code First Migrations: "Column names in each table must be unique" error after using RenameColumn
body: <p>We're using Entity Framework 5.0 Code First and Automatic Migrations.</p>
  
  <p>I had a class like so:</p>
  
  <pre><code>public class TraversalZones
  {
      public int Low { get; set; }
      public int High { get; set; }
  }​
  </code></pre>
  
  <p>Then we realized these properties weren't really the right names, so we changed them:</p>
  
  <pre><code>public class TraversalZones
  {
      public int Left { get; set; }
      public int Top { get; set; }
  }​
  </code></pre>
  
  <p>The rename refactored properly throughout the project, but I know Automatic Migrations aren't smart enough to pick up these explicit renames in the IDE, so I first checked to verify the only pending migration was this column rename:</p>
  
  <pre><code>update-database -f -script
  </code></pre>
  
  <p>Sure enough it just showed the SQL dropping Low and High and adding Left and Top. I then added a manual migration:</p>
  
  <pre><code>add-migration RenameColumns_TraversalZones_LowHigh_LeftTop
  </code></pre>
  
  <p>And fixed up the generated code to simply:</p>
  
  <pre><code>public override void Up()
  {
      RenameColumn("TraversalZones", "Low", "Left");
      RenameColumn("TraversalZones", "High", "Top");
  }
  
  public override void Down()
  {
      RenameColumn("TraversalZones", "Left", "Low");
      RenameColumn("TraversalZones", "Top", "High");
  }
  </code></pre>
  
  <p>​
  I then updated the db:</p>
  
  <pre><code>update-database -verbose
  </code></pre>
  
  <p>And got 2 column renames, just like I was expecting.</p>
  
  <p>Several migrations later I backed up Production and Restored it to a local DB to test the code on this DB. This DB had the TraversalZones table already created in it, with the old column names (Low and High) I of course began by updating it:</p>
  
  <pre><code>update-database -f -verbose
  </code></pre>
  
  <p>And the rename commands appeared in the output - all appeared well:</p>
  
  <pre><code>EXECUTE sp_rename @objname = N'TraversalZones.Low', @newname = N'Left', @objtype = N'COLUMN'
  EXECUTE sp_rename @objname = N'TraversalZones.High', @newname = N'Top', @objtype = N'COLUMN'
  [Inserting migration history record]
  </code></pre>
  
  <p>I then ran my code, and it errored out telling me the database had changed since last run, and that I should run <code>update-database</code>... .</p>
  
  <p>So I ran it again:</p>
  
  <pre><code>update-database -f -verbose
  </code></pre>
  
  <p>And am now stuck on this error:</p>
  
  <pre><code>No pending code-based migrations. Applying automatic migration:
  201212191601545_AutomaticMigration.
  ALTER TABLE [dbo].[TraversalZones] ADD [Left] [int] NOT NULL DEFAULT 0
  System.Data.SqlClient.SqlException (0x80131904): Column names in each table must be unique. Column name 'Left' in table 'dbo.TraversalZones' is specified more than once.
     at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
     at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
     at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
     at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean&amp; dataReady)
     at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout)
     at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
     at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
     at System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
     at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement)
     at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements)
     at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements)
     at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable`1 operations, Boolean downgrading, Boolean auto)
     at System.Data.Entity.Migrations.DbMigrator.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading)
     at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading)
     at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
     at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
     at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
     at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
     at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.RunCore()
     at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
  ClientConnectionId:c40408ee-def3-4553-a9fb-195366a05fff
  Column names in each table must be unique. Column name 'Left' in table 'dbo.TraversalZones' is specified more than once.​
  </code></pre>
  
  <p>So, clearly Migrations is confused as to whether the column "Left" still needs to make it into this table; I would assume RenameColumn would leave things in the proper state, but it appears it has not.</p>
  
  <p>When I dump what it's attempting to do to a <code>update-database -f -script</code>, I get it trying to do exactly what it would have done if the manual migration were not there:</p>
  
  <pre><code>ALTER TABLE [dbo].[TraversalZones] ADD [Left] [int] NOT NULL DEFAULT 0
  ALTER TABLE [dbo].[TraversalZones] ADD [Top] [int] NOT NULL DEFAULT 0
  DECLARE @var0 nvarchar(128)
  SELECT @var0 = name
  FROM sys.default_constraints
  WHERE parent_object_id = object_id(N'dbo.TraversalZones')
  AND col_name(parent_object_id, parent_column_id) = 'Low';
  IF @var0 IS NOT NULL
      EXECUTE('ALTER TABLE [dbo].[TraversalZones] DROP CONSTRAINT ' + @var0)
  ALTER TABLE [dbo].[TraversalZones] DROP COLUMN [Low]
  DECLARE @var1 nvarchar(128)
  SELECT @var1 = name
  FROM sys.default_constraints
  WHERE parent_object_id = object_id(N'dbo.TraversalZones')
  AND col_name(parent_object_id, parent_column_id) = 'High';
  IF @var1 IS NOT NULL
      EXECUTE('ALTER TABLE [dbo].[TraversalZones] DROP CONSTRAINT ' + @var1)
  ALTER TABLE [dbo].[TraversalZones] DROP COLUMN [High]
  INSERT INTO [__MigrationHistory] ([MigrationId], [Model], [ProductVersion]) VALUES ('201212191639471_AutomaticMigration', 0x1F8B08000...000, '5.0.0.net40')
  </code></pre>
  
  <p>This appears to be a bug in Migrations.</p>