How to drop a table in Entity Framework Code First?

前提是你 提交于 2019-12-01 16:47:25

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.

CodeMad

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.

Add AutomaticMigrationDataLossAllowed = true; to the Configuration class and it will drop tables automatically.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!