Code First 數據遷移

遷移」是一組有序的步驟,描述如何升級(和降級)數據庫架構。數據庫

 

當模型發生變化時

一、「工具」->「NuGet包管理器」->「程序包管理器控制檯」
在程序包管理器控制檯中運行 Enable-Migrations 命令架構

PM> Enable-Migrations
Checking if the context targets an existing database...
PM>

生成一個 Migrations 文件夾,它包含1個文件:Configuration.cs app

 

二、在程序包管理器控制檯中運行 Add-Migration XX 命令。工具

XX 是名稱,例如ddthis

PM> Add-Migration dd
Scaffolding migration 'dd'.
The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration dd' again.
PM>

在文件夾 Migrations 中生成 文件:202108071428537_dd.cs ,前面是時間spa

 

三、在程序包管理器控制檯中運行 Update-Database 命令。此命令將全部掛起的遷移應用於數據庫。blog

PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [202108071428537_dd].
Applying explicit migration: 202108071428537_dd.
Running Seed method.
PM>

  

 添加新模型,例如UserAA

一、定義UserAA模型類;ci

二、在DbContext 上下文中定義 public DbSet<UserAA> UserAA { get; set; }get

三、按照上面的步驟遷移it

 

刪除1個模型,例如UserAA

一、刪除UserAA模型類;

二、在DbContext 上下文中刪除 public DbSet<UserAA> UserAA { get; set; }

三、按照上面的步驟遷移

相關文章
相關標籤/搜索