如何删除表中的实体框架代码优先? [英] How to drop a table in Entity Framework Code First?

查看:182
本文介绍了如何删除表中的实体框架代码优先?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架与自动迁移。

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.

希望这会有所帮助。

这篇关于如何删除表中的实体框架代码优先?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆