Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop a table in Entity Framework Code First?

I am using Entity Framework with Auto Migrations.

So when I add a new model to my Context, my database is updated and new table is created.

What I want to do is the opposite, droping the table completely from database. However, removing the definition from Context class does not work.

public class CompanyContext : DbContext
{
    public DbSet<Permission> Permissions { get; set; }
    public DbSet<Company> Companies { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
       base.OnModelCreating(modelBuilder);
    }
}

For instance, I want to remove Company table from database. To do that, I remove Companies property from CompanyContext class. However, it is not working.

What is the proper way to drop tables in EF and using auto migrations if possible ?

like image 264
emre nevayeshirazi Avatar asked Oct 19 '13 23:10

emre nevayeshirazi


People also ask

How do you drop a table in EF core code first?

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

How do I drop a table in Entity Framework?

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.

How do I delete a table in EDMX?

In the . edmx, right-click in the area between tables and select Model Browser. It will open a file tree of all the tables contained in your model. Delete every instance of the table you changed in your database.


2 Answers

The add-migrations command creates a Migrations folder. You can see [DateStamp]_InitialCreate.cs file containing two methods viz.; Up and Down. The Up method of the InitialCreate class creates the database tables that corresponds to the data model entity sets, and the Down method delete them. Typically when you enter a command to rollback a database, Down method is called. You can see statements like DropIndex, DropForeignKey, DropTable statements in Down method.

In case of question asked by Emre, write the DropTable statement in the Down method of [DateStamp]_InitialCreate.cs class and the table will be dropped.

Hopefully it will help.

like image 114
Abhishek Sathe Avatar answered Oct 19 '22 11:10

Abhishek Sathe


You should implement your "IDatabaseInitializer" and create custom logic to do this operation. For example, please visit this

Also please see "How do I use Entity Framework in Code First Drop-Create mode?" if it can help.

In my own experience, I have done it by running the below statement from Package console manager of VS 2010

update-database -StartupProjectName "Your Project Namespace" -script -Verbose –force

Make sure you have "Your Project Namespace" selected as a default project.

like image 24
CodeMad Avatar answered Oct 19 '22 13:10

CodeMad