Search code examples
asp.netentity-frameworkmigrationcode-firstentity-framework-migrations

Entity Framework Code First Migration - drop column


When needing to add a new column to a table using Entity Framework Code First Migrations, you simply do something like: "add-migration newColumn", then "update-database". What is the specific command to drop newColumn?

"remove-migration newColumn", then "update-database"?


Solution

  • Your question is very confusing, are you trying to revert a migration or are you trying to simply drop a column that you previously added?

    Simple answer, modify your model (by removing the column) and add a new migration. This new migration will include the DropColumn command to remove the column.

    If you are trying to revert a migration, you need to:

    update-database -TargetMigration: "Migration" 
    

    Where "Migration" is the name of the migration prior to the one you are trying to revert. Additionally, if you need to revert to the original state you can use:

    update-database -TargetMigration: $InitialDatabase 
    

    The MSDN article explains this pretty well: https://msdn.microsoft.com/en-us/data/jj591621.aspx