title: EF Core Migration Failure: Error Number:4902,State:1,Class:16
body: <p>I recently rolled back an EF Core migration, which deleted an existing database table, called the "Tag" table, with a Foreign Key to a "Subject" table.</p>
  
  <p>The "Subject" table has a one to many relationship with a "Tag" table.</p>
  
  <p>One Subject has many Tags.</p>
  
  <p>When attempting to "update-database" to re-establish (re-create) the Tag table, with the Foreign Key, I receive the following error:Error Number:4902,State:1,Class:16, and the migration fails.</p>
  
  <p>The Tag table does not exist.  It was deleted as a result of the migration rollback.</p>
  
  <pre><code>public class Subject
      {
          public int Id { get; set; }
  
          [Required]
          public string Name { get; set; }
  
          [Required]
          public string Color { get; set; }        
      }
  
  
     public class Tag
      {
          public int Id { get; set; }
          [Required]
          public string Description { get; set; }
          [Required]
          public string Narrative { get; set; }
  
          public int SubjectId { get; set; }
  
          [ForeignKey("SubjectId")]
          public virtual Subject Subject { get; set; }      
      }
  
  Here is my migration:
  
  public partial class AddTagToDb : Migration
      {
          protected override void Up(MigrationBuilder migrationBuilder)
          {
              migrationBuilder.DropForeignKey(
                  name: "FK_Tag_Subject_SubjectId",
                  table: "Tag");
  
              migrationBuilder.AlterColumn&lt;int&gt;(
                  name: "SubjectId",
                  table: "Tag",
                  nullable: false,
                  oldClrType: typeof(int),
                  oldNullable: true);
  
              migrationBuilder.AlterColumn&lt;string&gt;(
                  name: "Narrative",
                  table: "Tag",
                  nullable: false,
                  oldClrType: typeof(string),
                  oldNullable: true);
  
              migrationBuilder.AlterColumn&lt;string&gt;(
                  name: "Description",
                  table: "Tag",
                  nullable: false,
                  oldClrType: typeof(string),
                  oldNullable: true);
  
              migrationBuilder.AddForeignKey(
                  name: "FK_Tag_Subject_SubjectId",
                  table: "Tag",
                  column: "SubjectId",
                  principalTable: "Subject",
                  principalColumn: "Id",
                  onDelete: ReferentialAction.Cascade);
          }
  
          protected override void Down(MigrationBuilder migrationBuilder)
          {
              migrationBuilder.DropForeignKey(
                  name: "FK_Tag_Subject_SubjectId",
                  table: "Tag");
  
              migrationBuilder.AlterColumn&lt;int&gt;(
                  name: "SubjectId",
                  table: "Tag",
                  nullable: true,
                  oldClrType: typeof(int));
  
              migrationBuilder.AlterColumn&lt;string&gt;(
                  name: "Narrative",
                  table: "Tag",
                  nullable: true,
                  oldClrType: typeof(string));
  
              migrationBuilder.AlterColumn&lt;string&gt;(
                  name: "Description",
                  table: "Tag",
                  nullable: true,
                  oldClrType: typeof(string));
  
              migrationBuilder.AddForeignKey(
                  name: "FK_Tag_Subject_SubjectId",
                  table: "Tag",
                  column: "SubjectId",
                  principalTable: "Subject",
                  principalColumn: "Id",
                  onDelete: ReferentialAction.Restrict);
          }
  
  Error message encountered after update-migrtation:
  
  PM&gt; update-database
  Microsoft.EntityFrameworkCore.Infrastructure[10403]
        Entity Framework Core 2.2.6-servicing-10079 initialized 'ApplicationDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
  Microsoft.EntityFrameworkCore.Database.Command[20101]
        Executed DbCommand (4ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
        SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
  Microsoft.EntityFrameworkCore.Database.Command[20101]
        Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
        SELECT OBJECT_ID(N'[__EFMigrationsHistory]');
  Microsoft.EntityFrameworkCore.Database.Command[20101]
        Executed DbCommand (0ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
        SELECT [MigrationId], [ProductVersion]
        FROM [__EFMigrationsHistory]
        ORDER BY [MigrationId];
  Applying migration '20190804003447_AddTagToDb'.
  Microsoft.EntityFrameworkCore.Migrations[20402]
        Applying migration '20190804003447_AddTagToDb'.
  fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
        Failed executing DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
        ALTER TABLE [Tag] DROP CONSTRAINT [FK_Tag_Subject_SubjectId];
  System.Data.SqlClient.SqlException (0x80131904): Cannot find the object "Tag" because it does not exist or you do not have permissions.
     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, Boolean asyncWrite)
     at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
     at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
     at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
  ClientConnectionId:4c476313-8831-48bc-b88d-e6979ff701bf
  Error Number:4902,State:1,Class:16
  Failed executing DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
  ALTER TABLE [Tag] DROP CONSTRAINT [FK_Tag_Subject_SubjectId];
  System.Data.SqlClient.SqlException (0x80131904): Cannot find the object "Tag" because it does not exist or you do not have permissions.
     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, Boolean asyncWrite)
     at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)
     at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
     at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)
     at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
     at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
     at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
     at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
     at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
     at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.&lt;&gt;c__DisplayClass0_1.&lt;.ctor&gt;b__0()
     at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
  ClientConnectionId:4c476313-8831-48bc-b88d-e6979ff701bf
  Error Number:4902,State:1,Class:16
  Cannot find the object "Tag" because it does not exist or you do not have permissions.
  </code></pre>