【问题标题】:How to drop a table in Entity Framework Code First?如何在 Entity Framework Code First 中删除表?
【发布时间】:2013-10-28 15:17:03
【问题描述】:

我正在使用具有自动迁移功能的实体框架。

因此,当我向 Context 添加新模型时,我的数据库会更新并创建新表。

我想要做的是相反的,从数据库中完全删除表。但是,从 Context 类中删除定义不起作用。

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);
    }
}

例如,我想从数据库中删除 Company 表。为此,我从 CompanyContext 类中删除了 Companies 属性。但是,它不起作用。

如果可能,在 EF 中删除表并使用自动迁移的正确方法是什么?

【问题讨论】:

    标签: c# entity-framework ef-code-first database-migration entity-framework-migrations


    【解决方案1】:

    add-migrations 命令创建一个 Migrations 文件夹。您可以看到 [DateStamp]_InitialCreate.cs 文件包含两个方法,即;上和下。 InitialCreate 类的 Up 方法创建与数据模型实体集对应的数据库表,Down 方法删除它们。通常,当您输入命令回滚数据库时,会调用 Down 方法。在 Down 方法中可以看到 DropIndex、DropForeignKey、DropTable 等语句。

    如果Emre提出问题,请在[DateStamp]_InitialCreate.cs类的Down方法中写入DropTable语句,表格将被删除。

    希望它会有所帮助。

    【讨论】:

      【解决方案2】:

      您应该实现“IDatabaseInitializer”并创建自定义逻辑来执行此操作。例如,请访问this

      如果有帮助,请参阅“How do I use Entity Framework in Code First Drop-Create mode?”。

      根据我自己的经验,我是通过从 VS 2010 的包控制台管理器中运行以下语句来完成的

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

      确保您已选择“您的项目命名空间”作为默认项目。

      【讨论】:

      • 据我所知,DropCreateDatabaseAlways 删除整个数据库并重新创建它。结果,您丢失了数据。我想要做的是只删除单个表。
      • 那你为什么不更新你的模型并运行上面提到的包管理器控制台命令呢?
      • 我做到了。我从 DbContext 类中删除了模型类,并且还删除了模型文件本身。我认为自动迁移应该处理其余的。不是。
      • 在进一步分析之前,我只是有一个盲目的想法。您可以使用 DropCreateDatabaseAlways 删除和重新创建数据库,并让您的代码自动运行 sql 种子文件以插入数据。 (您也可以在运行“DropCreateDatabaseAlways”之前生成脚本)
      【解决方案3】:

      AutomaticMigrationDataLossAllowed = true; 添加到配置类,它会自动删除表。

      【讨论】:

        【解决方案4】:

        我也有类似的问题。 假设您的初始迁移创建了一个名为“SampleTable”的表

            public partial class InitialCreate : Migration
            {
                protected override void Up(MigrationBuilder migrationBuilder)
                {
                    migrationBuilder.CreateTable(
                        name: "SampleTable",
                        columns: table => new
                        {
                            ...
                        },
                        constraints: table =>
                        {
                            table.PrimaryKey("PK_SampleTable", x => x.Id);
                        });       
                }
        
                protected override void Down(MigrationBuilder migrationBuilder)
                {
                    migrationBuilder.DropTable(
                        name: "SampleTable");
                }
            }
        
        

        而你想删除那张桌子。您可能需要遵循的步骤是

        1. dbcontext 中删除模型。
        2. 运行命令dotnet ef migrations add RemoveSampleTable(或任何迁移名称)。但它只会产生空迁移。
            public partial class RemoveSampleTable : Migration
            {
                protected override void Up(MigrationBuilder migrationBuilder)
                {
                }
        
                protected override void Down(MigrationBuilder migrationBuilder)
                {
                }
            }
        
        1. 如果您通过执行类似dotnet ef database update 的命令来应用迁移。它基本上什么都不做。所以这里的诀窍是从早期迁移中复制 Up/Down 的内容并将其粘贴到相反的方法中。
            public partial class RemoveSampleTable : Migration
            {
                protected override void Up(MigrationBuilder migrationBuilder)
                {
                    migrationBuilder.DropTable(
                        name: "VesselBuildingProject");
                }
        
                protected override void Down(MigrationBuilder migrationBuilder)
                {
                            migrationBuilder.CreateTable(
                                name: "SampleTable",
                                columns: table => new
                                {
                                    ...
                                },
                                constraints: table =>
                                {
                                    table.PrimaryKey("PK_SampleTable", x => x.Id);
                                });
                }
            }
        
        1. 现在,如果您现在已经在数据库上尝试过迁移,则需要从 __EFMigrationsHistory 中删除该条目。
        2. 现在继续执行命令dotnet ef database update 看看魔法:P

        【讨论】:

          猜你喜欢
          • 2022-01-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-07-29
          相关资源
          最近更新 更多