title: Migration generated by Entity Framework crashes when applying migration for one to one relation to database
body: <p>I have a rather large model in which we had to shuffle a foreign key relation around. Apart from changing the relations the migration shouldn't do much. However, the behavior we observe is that the generated migration is bogus, in fact, it attempts to drop the primary key of the table. Which obviously fails. </p>
  
  <p>The old situation was the following:</p>
  
  <pre><code>public class InvoiceStatus
  {
      public int Id { get; set; }
  }
  
  public class InvoiceSample 
  {
      public int Id { get; set; }
  
      public int InvoiceStatusId { get; set; }
  
      public InvoiceStatus InvoiceStatus { get; set; }
  }
  </code></pre>
  
  <p>This has now been changed to:</p>
  
  <pre><code>public class InvoiceStatus
  {
      public int Id { get; set; }
  
      public int? InvoiceSampleId { get; set; }
  
      public virtual InvoiceSample InvoiceSample{ get; set; }
  }
  
  public class InvoiceSample 
  {
      public int Id { get; set; }
  
      public int InvoiceStatusId { get; set; }
  
      public InvoiceStatus InvoiceStatus { get; set; }
  }
  </code></pre>
  
  <p>In addition to changing the entities themselves we added the following snippet to the <code>ModelBuilder</code>: </p>
  
  <pre><code>        modelBuilder.Entity&lt;InvoiceStatus&gt;()
                              .HasOptional(st =&gt; st.InvoiceSample)
                              .WithRequired(smp =&gt; smp.InvoiceStatus);
  </code></pre>
  
  <p>The migration generated for this change contains the following lines: </p>
  
  <pre><code>    public override void Up()
      {
          DropForeignKey("dbo.InvoiceSamples", "InvoiceStatusId", "dbo.InvoiceStatus");
          DropIndex("dbo.InvoiceSamples", new[] { "InvoiceStatusId" });
          DropColumn("dbo.InvoiceSamples", "Id");
          RenameColumn(table: "dbo.InvoiceSamples", name: "InvoiceStatusId", newName: "Id");
          DropPrimaryKey("dbo.InvoiceSamples");
          AddColumn("dbo.InvoiceStatus", "InvoiceSampleId", c =&gt; c.Int());
          AlterColumn("dbo.InvoiceSamples", "Id", c =&gt; c.Int(nullable: false, identity: true));
          AddPrimaryKey("dbo.InvoiceSamples", "Id");
          CreateIndex("dbo.InvoiceSamples", "Id");
          CreateIndex("dbo.InvoiceSamples", "InvoiceStatusId", unique: true);
          AddForeignKey("dbo.InvoiceStatus", "SetId", "dbo.InvoiceSets", "Id", cascadeDelete: true);
          AddForeignKey("dbo.InvoiceSamples", "Id", "dbo.InvoiceStatus", "Id");;
      }
  </code></pre>
  
  <p>Obviously this migration is not going to fly, as EF attempts to drop the primary key while it is still the primary key. </p>
  
  <p>Additionally a migration I attempted to generate after the above migration also failed when executing. This second migration was to drop a foreign key relation between two entities. Manually executing the generated SQL statements one-by-one on the database worked while the automatic migration failed with an SQL Exception. </p>
  
  <p>Is there any reason why EF in this case has such a considerable effort generating and executing valid migrations? Or is there something I am missing here? </p>