title: Entity Framework Update-Database issues
body: <p>I have been using Entity Framework Code First approach for a few years now, and I must say, I have been impressed, by how easy it seed to maintain - until now!</p>
  
  <p>In my new project, I seem to run in to problems, everytime I alter an Entity, which references other entities.</p>
  
  <p>When I run the update-database -verbose function in the Packet-Manager Console, I seem to get the same error.</p>
  
  <pre><code>Cannot find the object xxxx because it does not exist or you do not have permissions.
  </code></pre>
  
  <p>In my current case, I have a collection of Cars, and I wanted to add sort order to the collection, so I added a new class called CarSortPair, which takes a Car object, a SortOrder integer, and which has an ID also.</p>
  
  <p>I have tried searching a lot on Google and StackOverflow, and there are different suggestions, and many of them suggest to run add-migration.</p>
  
  <p>When I run add-migration carlist, i get this code into the automatically generated migration class.</p>
  
  <pre><code>public partial class carlist : DbMigration
  {
      public override void Up()
      {
          RenameTable(name: "dbo.CarListCars", newName: "CarListCarSortPairs");
          DropForeignKey("dbo.CarListCars", "Car_ID", "dbo.Cars");
          DropIndex("dbo.CarListCars", new[] { "Car_ID" });
          CreateTable(
              "dbo.CarSortPairs",
              c =&gt; new
                  {
                      ID = c.Int(nullable: false, identity: true),
                      SortOrder = c.Int(nullable: false),
                      Car_ID = c.Int(),
                  })
              .PrimaryKey(t =&gt; t.ID)
              .ForeignKey("dbo.Cars", t =&gt; t.Car_ID)
              .Index(t =&gt; t.Car_ID);
  
          CreateIndex("dbo.CarListCarSortPairs", "CarSortPair_ID");
          AddForeignKey("dbo.CarListCarSortPairs", "CarSortPair_ID", "dbo.CarSortPairs", "ID", cascadeDelete: true);
      }
  
      public override void Down()
      {
          DropForeignKey("dbo.CarListCarSortPairs", "CarSortPair_ID", "dbo.CarSortPairs");
          DropForeignKey("dbo.CarSortPairs", "Car_ID", "dbo.Cars");
          DropIndex("dbo.CarListCarSortPairs", new[] { "CarSortPair_ID" });
          DropIndex("dbo.CarSortPairs", new[] { "Car_ID" });
          DropTable("dbo.CarSortPairs");
          CreateIndex("dbo.CarListCars", "Car_ID");
          AddForeignKey("dbo.CarListCars", "Car_ID", "dbo.Cars", "ID", cascadeDelete: true);
          RenameTable(name: "dbo.CarListCarSortPairs", newName: "CarListCars");
      }
  }
  </code></pre>
  
  <p>Which at first did not seem strange for me, but when I investigated it closer, I noticed, that the order of the script is not right!?</p>
  
  <p>I tried to run update-database -verbose -script, which generated this script:</p>
  
  <pre><code>EXECUTE sp_rename @objname = N'dbo.CarListCars', @newname = N'CarListCarSortPairs', @objtype = N'OBJECT'
  IF object_id(N'[dbo].[FK_dbo.CarListCars_dbo.Cars_Car_ID]', N'F') IS NOT NULL
      ALTER TABLE [dbo].[CarListCars] DROP CONSTRAINT [FK_dbo.CarListCars_dbo.Cars_Car_ID]
  IF EXISTS (SELECT name FROM sys.indexes WHERE name = N'IX_Car_ID' AND object_id = object_id(N'[dbo].[CarListCars]', N'U'))
      DROP INDEX [IX_Car_ID] ON [dbo].[CarListCars]
  CREATE TABLE [dbo].[CarSortPairs] (
      [ID] [int] NOT NULL IDENTITY,
      [SortOrder] [int] NOT NULL,
      [Car_ID] [int],
      CONSTRAINT [PK_dbo.CarSortPairs] PRIMARY KEY ([ID])
  )
  CREATE INDEX [IX_Car_ID] ON [dbo].[CarSortPairs]([Car_ID])
  CREATE INDEX [IX_CarSortPair_ID] ON [dbo].[CarListCarSortPairs]([CarSortPair_ID])
  ALTER TABLE [dbo].[CarListCarSortPairs] ADD CONSTRAINT [FK_dbo.CarListCarSortPairs_dbo.CarSortPairs_CarSortPair_ID] FOREIGN KEY ([CarSortPair_ID]) REFERENCES [dbo].[CarSortPairs] ([ID]) ON DELETE CASCADE
  ALTER TABLE [dbo].[CarSortPairs] ADD CONSTRAINT [FK_dbo.CarSortPairs_dbo.Cars_Car_ID] FOREIGN KEY ([Car_ID]) REFERENCES [dbo].[Cars] ([ID])
  INSERT [dbo].[__MigrationHistory]([MigrationId], [ContextKey], [Model], [ProductVersion])
  VALUES (N'201312100911087_AutomaticMigration', ......
  </code></pre>
  
  <p>Then I tried to execute the SQL script, line by line, and it indeed is executed in wrong order!</p>
  
  <p>How can I fix this, in a way, that I can maintain my code easily. It does not make sense, if I need to maintain the scripts manually.</p>
  
  <p>Edit: I have tried to update to EF 6.0.1 and the newest 6.0.2-beta2, to see if this was an error in the Entity Framework, but same result.</p>