簡體   English   中英

如何在實體框架代碼優先中刪除表?

[英]How to drop a table in Entity Framework Code First?

我正在使用帶有自動遷移的實體框架。

因此,當我向上下文添加新模型時,我的數據庫會更新並創建新表。

我想要做的是相反的,從數據庫中完全刪除表。 但是,從 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 中的表並使用自動遷移的正確方法是什么?

add-migrations 命令創建一個 Migrations 文件夾。 您可以看到 [DateStamp]_InitialCreate.cs 文件包含兩個方法,即。 上下。 InitialCreate 類的Up 方法創建與數據模型實體集對應的數據庫表,Down 方法刪除它們。 通常,當您輸入回滾數據庫的命令時,會調用 Down 方法。 您可以在 Down 方法中看到 DropIndex、DropForeignKey、DropTable 等語句。

如果Emre提出問題,在[DateStamp]_InitialCreate.cs類的Down方法中寫入DropTable語句,該表將被刪除。

希望它會有所幫助。

您應該實現您的“IDatabaseInitializer”並創建自定義邏輯來執行此操作。 例如,請訪問

另請參閱“ 如何在 Code First Drop-Create 模式下使用實體框架? ”是否有幫助。

根據我自己的經驗,我是通過從 VS 2010 的包控制台管理器運行以下語句來完成的

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

確保將“您的項目命名空間”選為默認項目。

添加AutomaticMigrationDataLossAllowed = true; 到 Configuration 類,它會自動刪除表。

我也有類似的問題。 假設您的初始遷移創建了一個名為“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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM