Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete table from EF CodeFirst migration

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?

like image 669
DevPerson Avatar asked Aug 12 '16 04:08

DevPerson


People also ask

How to delete a database in EF code first?

I believe the aim is to delete the database itself and recreate it using EF Code First approach. 1.Open your project in Visual Studio using the ".sln" extention. 2.Select Server Explorer ( it is oftentimes on the left) 3.Select SQL Server Object Explorer. 4.The database you want to delete would be listed under any of the localDB.

How do I create a new migration table in codefirstfromexistingdb?

A new migrations table will now have been created in the CodeFirstFromExistingDB database. Following this, add a new property named Description to the Product class, with a maximum allowed length of 50 characters, as follows: public int? CategoryID { get; set; }

How do I remove the last migration in EF6?

Using EF6 with ASP.Net Core 5 I found these commands handy during first initialization of the database: Remove-Migration -force; Add-Migration InitialMigration; Update-Database; It removes the last migration (should be the only one), creates it again, then refreshes the database.

How do I remove a table from an EF model?

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.


3 Answers

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; } 
like image 30
Chameera Avatar answered Sep 21 '22 17:09

Chameera


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.

like image 107
Steve Greene Avatar answered Sep 23 '22 17:09

Steve Greene


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

like image 30
woodykiddy Avatar answered Sep 20 '22 17:09

woodykiddy