12

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?

Termininja
  • 6,167
  • 12
  • 45
  • 48
DevPerson
  • 369
  • 1
  • 5
  • 19

4 Answers4

11

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.

Community
  • 1
  • 1
Steve Greene
  • 11,559
  • 1
  • 29
  • 48
3

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

Phil G
  • 67
  • 1
  • 8
woodykiddy
  • 5,812
  • 13
  • 52
  • 96
  • 1
    Using DropTable is correct but it's probably better to adjust the model. Also the question seems to be about deleting a table not undoing a migration. Down() is for reversing the latest migration. So from my understanding the code should be in the Up() in this case. – Mikael Eliasson Aug 14 '16 at 06:51
  • @MikaelEliasson Yes, Thanks. Done :) – DevPerson Aug 16 '16 at 14:07
3
  1. Delete from your context class this line

    public DbSet TableClassNameSet { get; set; }

  2. Execute this command:

    Update-DataBase -force -verbose

2

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; } 
Tasos K.
  • 7,831
  • 7
  • 41
  • 61
Chameera
  • 395
  • 2
  • 8
  • In my case, I'd got stuck after removing the unwanted models, deleting their public virtual DbSet<> entires, migrating and updating the database. It was all fine until I tried another migration, at which point the whole thing (MySQL/.NET Core 5) was broken wrt earlier migrations not finding the required models. I had to revert that commit and just leave the models in place, removing only the DbSet<> statements as above. Migrations and Updates have worked since. – cherry Jul 21 '21 at 09:45