Delete table from EF CodeFirst migration

24,038

Solution 1

If you just made the changes in the last migration, you can rollback that migration. Otherwise, just adjust your models and the changes will be picked up in the next migration. To remove a table, simply remove the corresponding DbSet<MyClass> and any references to that class in other parts of your model and EF will add a DropTable to the migration automatically. If you are no longer using the class for non-Entity Framework purposes you can delete it.

Solution 2

To drop a table, you can use DropTable("YourTable") in the Up() method of your DBMigration class.

Also take a look at the following link for more examples on how to customize the migration.

https://msdn.microsoft.com/en-au/data/jj591621.aspx#customizing

Solution 3

  1. Delete from your context class this line

    public DbSet TableClassNameSet { get; set; }

  2. Execute this command:

    Update-DataBase -force -verbose

Solution 4

f you just made the changes in the last migration, you can rollback that migration. Otherwise, just adjust your models and the changes will be picked up in the next migration. To remove a table, simply remove the corresponding DbSet<MyClass> and any references to that class in other parts of your model and EF will add a DropTable to the migration automatically. If you are no longer using the class for non-Entity Framework purposes you can delete it.

remove your

public DbSet<TableClassName> TableClassNameSet { get; set; } 
Share:
24,038
DevPerson
Author by

DevPerson

Updated on July 05, 2022

Comments

  • DevPerson
    DevPerson almost 2 years

    In EF Code first, I want to drop one column from one table & then delete another table.

    After removing one column from class file, automatically one migration file generated.

    But how to delete table.

    what command need to fire? Do I need to delete complete class file & also remove following line from Context file?

     public DbSet<TableClassName> TableClassNameSet { get; set; }
    

    I use, Add Migration 'TableClassName' command.

    So what is best way to remove table?