1. 程式人生 > >ef資料遷移命令總結之Add-Migration

ef資料遷移命令總結之Add-Migration

ef資料遷移命令總結之Add-Migration

首先我們可以在vs的程式包管理控制檯輸入 get-help Add-Migration -detailed以檢視詳細資訊。

個人感覺有一篇好的文章,http://www.mortenanderson.net/code-first-migrations-for-entity-framework ,
在本人的感覺就是 其對這些命令的掌握很深刻,所以在專案中摘錄好的地方進行翻譯,如果侵權,請聯絡刪除。


Add-Migration 
-Name LearningMpaAbp.EntityFramework 
-Force 
-ProjectName LearningMpaAbp.EntityFramework  
-StartUpProjectName ConsoleApp1  
-ConfigurationTypeName  LearningMpaAbp.Migrations.Configuration 
-ConnectionStringName Default  


Add-Migration 
-Name LearningMpaAbp.EntityFramework 
-Force 
-ProjectName LearningMpaAbp.EntityFramework  
-StartUpProjectName ConsoleApp1  
-ConfigurationTypeName  LearningMpaAbp.Migrations.Configuration 
<!-- 如下和上邊的 -ConnectionStringName Default  的作用是一樣的 -->
-ConnectionString "data source=DESKTOP-H6LD3FA\SQLEXPRESS;Database=LearningMpaAbp;User Id=sa;Password=123456;" -ConnectionProviderName System.Data.SqlClient

名稱Add-Migration

The add-migration command is one of the key commands in code first migrations. When you make changes to your domain model and need them dded into your database you create a new migration. This is done with the Add-Migration command. In it’s simplest form you need only toprovide a migration name.

add-migration命令是code first migration中的關鍵命令之一。當您對領域域模型進行更改並需要將它們時新增到資料庫中,您將建立一個新的遷移。這是通過Add-Migration命令完成的。用最簡單的形式,你只需要提供遷移名稱。

The Add-Migration command scaffolds your changes into a cs file. This cs file is placed in the same folder as the configuration file for the DbContext you are targeting.

Add-Migration命令將您的更改構建到一個cs檔案中。這個cs檔案與配置檔案放在同一個資料夾中,服務於您要瞄準的DbContext。

摘要
Scaffolds a migration script for any pending model changes.
為任何待定的模型更改 搭建遷移指令碼。

語法

    Add-Migration
     [-Name] <String> 
     [-Force] [-ProjectName <String>] 
     [-StartUpProjectName <String>] 
     [-ConfigurationTypeName <String>] 
     [-ConnectionStringName <String>] 
     [-IgnoreChanges] 
     [-AppDomainBaseDirectory <String>]
      [<CommonParameters>]
   Add-Migration 
    [-Force] [-ProjectName <String>]
    [-StartUpProjectName <String>]
    [-ConfigurationTypeName <String>] 
     -ConnectionString <String> 
    [-Name] <String> 
     -ConnectionProviderName <String> 
    [-IgnoreChanges] 
    [-AppDomainBaseDirectory <String>] 
    [<CommonParameters>]    

說明
Scaffolds a new migration script and adds it to the project.
搭建一個新的資料遷移指令碼 ,並把它新增到專案中。

引數
-Name

Specifies the name of the custom script.

指定自定義指令碼 的名字

-Force []

Specifies that the migration user code be overwritten when re-scaffolding an existing migration.

指定 當我們重新 搭建一個已經存在的資料遷移的時候,原本存在的資料遷移程式碼可以被重寫

If you want to re-scaffold an existing migration you will have to use the -Force parameter. However you can only re-scaffold if the migration is not yet applied to database. Else you will need to revert back to the migration previous to the one you want to re-scaffold.

如果要重新構建現有遷移,必須使用-Force引數。然而,只有在遷移尚未應用到資料庫時,才能重新構建框架。否則你 需要回復到要重新構建的遷移之前的遷移。

-ProjectName

Specifies the project that contains the migration configuration type to beused. If omitted, the default project selected in package manager console is used.

指定 包含了資料遷移配置型別的專案,如果被忽略,就會選擇包管理控制器中的預設專案。

If your migrations should not be placed in your default project you can utilize the -projectname parameter

-StartUpProjectName

Specifies the configuration file to use for named connection strings. If omitted, the specified project’s configuration file is used.

這個其實我也看不懂, 但實驗之後,大概是從 解決方案資源管理器中選擇一個專案 作為啟動專案。如果我們忽略的話,就會預設為解決方案資源管理器中的啟動專案。

-ConfigurationTypeName

Specifies the migrations configuration to use. If omitted, migrations willattempt to locate a single migrations configuration type in the target project.

這一段英文解釋了,大概還是不會懂。

If you have multiple DbContext in your project you will need to indicate which is going to have the database update. This can be done with -ConfigurationTypeName. The ConfigurationTypeName is the name of your Configuration class in your migration folder.

如果您的專案中有多個DbContext,那麼您需要指出哪個資料庫會更新。這可以用-ConfigurationTypeName做。ConfigurationTypeName方法是遷移資料夾中配置類的名稱。

然後是一個多個DbContext的例子
		
  First step
  ==========
  
  enable-migrations -ContextTypeName IdentityDb -MigrationsDirectory DAL\IdentityMigrations
  
  enable-migrations -ContextTypeName SMSContext -MigrationsDirectory DAL\SMSMigrations
  
  Second Step
  ===========
  
  add-migration -ConfigurationTypeName SMSApp.DAL.IdentityMigrations.Configuration "InitialCreate"
  
  add-migration -ConfigurationTypeName SMSApp.DAL.SMSMigrations.Configuration "InitialCreate"
  
  Third Step
  ==========
  
  update-database -ConfigurationTypeName SMSApp.DAL.IdentityMigrations.Configuration -verbose
  
  update-database -ConfigurationTypeName SMSApp.DAL.SMSMigrations.Configuration -verbose

-ConnectionStringName

Specifies the name of a connection string to use from the application’s configuration file.

從應用程式的配置檔案中指定要使用的連線字串的名字。我們用兩個引數-ConnectionString -ConnectionProviderName ,或者用這樣一個引數代替

-ConnectionString

Specifies the the connection string to use. If omitted, the context’s default connection will be used.

指定要使用的連線字串。如果省略,上將使用DbContext的預設連線。
例如"data source=DESKTOP-H6LD3FA\SQLEXPRESS;Database=LearningMpaAbp;User Id=sa;Password=123456;"

-ConnectionProviderName

指定ConnectionProviderName 例如 System.Data.SqlClient

-IgnoreChanges []

Scaffolds an empty migration ignoring any pending changes detected in the current model.This can be used to create an initial, empty migration to enable Migrations for an existing database. N.B. Doing this assumes that the target database schema is compatible with the current model.
這樣做假設目標資料庫模式與當前的模型是一致的。構建一個空遷移和對應的空的遷移檔案,忽略在當前模型中檢測到的任何掛起的更改。可用於建立初始的空遷移,以支援對現有資料庫的遷移。

-AppDomainBaseDirectory

Specifies the directory to use for the app-domain that is used for running Migrations code such that the app-domain is able to find all required assemblies. This is an advanced option that should only be needed if the solution contains several projects such that the assemblies needed for the context and configuration are not all referenced from either the project containing the context or the project containing the migrations.
指定用於執行資料遷移程式碼的app-domain的路徑,從而app-domain 可以找到所有需要的程式集。這是一個高階選項,只有當解決方案包含多個專案時才會需要。這樣的話,context和configuration所需要的程式集就不僅僅從那些包含context和包含migrations的專案中獲取。