我使用实体框架与自动迁移。
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.
我想要做的是相反的,从数据库中完全滴速表。然而,从Context类删除定义不起作用
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); } }
比如说,我想删除从数据库公司表。从 CompanyContext 类要做到这一点,我删除公司属性。然而,这是行不通的。
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.
什么是砸在EF表的正确方法,并使用自动迁移如果可能的话?
What is the proper way to drop tables in EF and using auto migrations if possible ?
推荐答案外接迁移命令创建一个文件夹迁移。你可以看到一个包含两个方法即; [日期戳] _InitialCreate.cs文件上和下。在InitialCreate类的最多的方法创建一个对应的数据模型实体集的数据库表,而向下的方法将它们删除。通常情况下,当你输入一个命令,回退数据库,唐氏方法被调用。你可以看到像DropIndex,DropForeignKey,唐氏方法DROPTABLE陈述。
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.
在问题被埃姆雷要求的情况下,写DROPTABLE声明的向下方法[日期戳] _InitialCreate.cs类和表将被丢弃。
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.
希望这会有所帮助。
更多推荐
如何删除表中的实体框架代码优先?
发布评论