Dropping a table using entity framework 6

ef-code-first entity-framework-6

Question

I'm working on a project using entity framework 6 code first and I am trying to work out the correct way to drop a now obsolete table from the database. There doesn't seem to be much information about this on the web, I know I could simply remove the dbset from my dbcontext, remove the migration file for it and the row from the migrations table in the db but I just wanted to see if that was the only way as this seems a lot of messing about?

1
3
5/28/2014 7:34:48 PM

Accepted Answer

If I understood correctly you are using migrations as well, so it would not be a good idea to simply remove a migration file as it would break the databases that has the newest migrations.

Instead create another migration step after you removed the related DbSet from the DbContext and of course all references of the entity, that has a T parameter of the DbSet, from other entities that are still in the database.

And when you create this new migration step, voila a DropTable(...) migration operation will appear in the Up() function that will remove this table.

I think this is the cleanest and easiest solution for your problem :)

5
9/20/2017 5:06:15 PM

Popular Answer

This is a little retro, but try the following. In this example, db is the database context and A is the table name:

db.Database.ExecuteSqlCommand("drop table A");

Seemed to work for me.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow