使用 migrate.exeUsing migrate.exe

Code First 迁移可用于从 visual studio 内部更新数据库,但也可以通过命令行工具 migrate.exe 来执行。Code First Migrations can be used to update a database from inside visual studio, but can also be executed via the command line tool migrate.exe. 本页将简要介绍如何使用 migrate.exe 来对数据库执行迁移。This page will give a quick overview on how to use migrate.exe to execute migrations against a database.

备注

本文假设你知道如何在基本方案中使用 Code First 迁移。This article assumes you know how to use Code First Migrations in basic scenarios. 如果没有,则需要先阅读 Code First 迁移 ,然后再继续。If you don’t, then you’ll need to read Code First Migrations before continuing.

复制 migrate.exeCopy migrate.exe

使用 NuGet 安装实体框架时 migrate.exe 会位于已下载包的 tools 文件夹内。When you install Entity Framework using NuGet migrate.exe will be inside the tools folder of the downloaded package. 在 < 项目文件夹 > \ 包 \ EntityFramework 中。 <版本 > \ 工具In <project folder>\packages\EntityFramework.<version>\tools

migrate.exe 后,需要将其复制到包含迁移的程序集所在的位置。Once you have migrate.exe then you need to copy it to the location of the assembly that contains your migrations.

如果应用程序面向 .NET 4,而不是4.5,则还需要将 Redirect.config 复制到该位置,并将其重命名 migrate.exe.config。这是为了 migrate.exe 获取正确的绑定重定向,以便能够找到实体框架的程序集。If your application targets .NET 4, and not 4.5, then you will need to copy the Redirect.config into the location as well and rename it migrate.exe.config. This is so that migrate.exe gets the correct binding redirects to be able to locate the Entity Framework assembly.

.NET 4.5.NET 4.5 .NET 4。0.NET 4.0
.NET 4.5 文件 .NET 4.0 文件

备注

migrate.exe 不支持 x64 程序集。migrate.exe doesn't support x64 assemblies.

将 migrate.exe 移动到正确的文件夹后,应该能够使用它来对数据库执行迁移。Once you have moved migrate.exe to the correct folder then you should be able to use it to execute migrations against the database. 此实用程序的目的是执行迁移。All the utility is designed to do is execute migrations. 它无法生成迁移或创建 SQL 脚本。It cannot generate migrations or create a SQL script.

查看选项See options

Migrate.exe /?

以上将显示与此实用程序关联的帮助页,请注意,你需要在运行的同一位置 migrate.exe EntityFramework.dll,才能使其正常运行。The above will display the help page associated with this utility, note that you will need to have the EntityFramework.dll in the same location that you are running migrate.exe in order for this to work.

迁移到最新迁移Migrate to the latest migration

Migrate.exe MyMvcApplication.dll /startupConfigurationFile="..\\web.config"

在运行时 migrate.exe 唯一的必需参数是程序集,该程序集是包含您尝试运行的迁移的程序集,但如果您未指定配置文件,则它将使用所有基于约定的设置。When running migrate.exe the only mandatory parameter is the assembly, which is the assembly that contains the migrations that you are trying to run, but it will use all convention based settings if you do not specify the configuration file.

迁移到特定迁移Migrate to a specific migration

Migrate.exe MyApp.exe /startupConfigurationFile="MyApp.exe.config" /targetMigration="AddTitle"

如果要运行到特定迁移的迁移,则可以指定迁移的名称。If you want to run migrations up to a specific migration, then you can specify the name of the migration. 这会根据需要运行所有以前的迁移,直到获取所指定的迁移。This will run all previous migrations as required until getting to the migration specified.

指定工作目录Specify working directory

Migrate.exe MyApp.exe /startupConfigurationFile="MyApp.exe.config" /startupDirectory="c:\\MyApp"

如果程序集具有依赖项或读取相对于工作目录的文件,则需要设置 startupDirectory。If you assembly has dependencies or reads files relative to the working directory then you will need to set startupDirectory.

指定要使用的迁移配置Specify migration configuration to use

Migrate.exe MyAssembly CustomConfig /startupConfigurationFile="..\\web.config"

如果有多个迁移配置类,继承自 DbMigrationConfiguration 的类,则需要指定将用于此执行的类。If you have multiple migration configuration classes, classes inheriting from DbMigrationConfiguration, then you need to specify which is to be used for this execution. 这是通过提供可选的第二个参数(不带上述开关)来指定的。This is specified by providing the optional second parameter without a switch as above.

提供连接字符串Provide connection string

Migrate.exe BlogDemo.dll /connectionString="Data Source=localhost;Initial Catalog=BlogDemo;Integrated Security=SSPI" /connectionProviderName="System.Data.SqlClient"

如果要在命令行中指定连接字符串,则还必须提供提供程序名称。If you wish to specify a connection string at the command line then you must also provide the provider name. 如果未指定提供程序名称,将导致异常。Not specifying the provider name will cause an exception.

常见问题Common Problems

错误消息Error Message 解决方案Solution
未经处理的异常: FileLoadException:无法加载文件或程序集 "EntityFramework,Version = 5.0.0.0,Culture = 中立,PublicKeyToken = b77a5c561934e089" 或其依赖项之一。Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. 找到的程序集清单定义与程序集引用不匹配。The located assembly's manifest definition does not match the assembly reference. (HRESULT 中的异常: 0x80131040) (Exception from HRESULT: 0x80131040) 这通常意味着你在运行不带 Redirect.config 文件的 .NET 4 应用程序。This typically means that you are running a .NET 4 application without the Redirect.config file. 需要将 Redirect.config 复制到 migrate.exe 的同一位置,并将其重命名为 migrate.exe.config。You need to copy the Redirect.config to the same location as migrate.exe and rename it to migrate.exe.config.
未经处理的异常: FileLoadException:无法加载文件或程序集 "EntityFramework,Version = 4.4.0.0,Culture = 中立,PublicKeyToken = b77a5c561934e089" 或其依赖项之一。Unhandled Exception: System.IO.FileLoadException: Could not load file or assembly 'EntityFramework, Version=4.4.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. 找到的程序集清单定义与程序集引用不匹配。The located assembly's manifest definition does not match the assembly reference. (HRESULT 中的异常: 0x80131040) (Exception from HRESULT: 0x80131040) 此异常表示你运行的 .NET 4.5 应用程序 Redirect.config 复制到 migrate.exe 位置。This exception means that you are running a .NET 4.5 application with the Redirect.config copied to the migrate.exe location. 如果应用是 .NET 4.5,则不需要在内部重定向配置文件。If your app is .NET 4.5 then you do not need to have the config file with the redirects inside. 删除 migrate.exe.config 文件。Delete the migrate.exe.config file.
错误:无法更新数据库以匹配当前模型,因为存在挂起的更改,并且禁用了自动迁移。ERROR: Unable to update database to match the current model because there are pending changes and automatic migration is disabled. 将挂起的模型更改写入到基于代码的迁移或启用自动迁移。Either write the pending model changes to a code-based migration or enable automatic migration. 将 DbMigrationsConfiguration 设置为 true 以启用自动迁移。Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration. 如果尚未创建迁移来处理对模型所做的更改,并且数据库与模型不匹配,则会发生此错误。This error occurs if running migrate when you haven’t created a migration to cope with changes made to the model, and the database does not match the model. 将属性添加到模型类后,运行 migrate.exe 而不创建迁移来升级数据库就是这样的示例。Adding a property to a model class then running migrate.exe without creating a migration to upgrade the database is an example of this.
错误:未解析成员 "ToolingFacade + UpdateRunner,EntityFramework,Version = 5.0.0.0,Culture = 中立,PublicKeyToken = b77a5c561934e089" 的类型。ERROR: Type is not resolved for member 'System.Data.Entity.Migrations.Design.ToolingFacade+UpdateRunner,EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. 此错误的原因可能是指定了错误的启动目录。This error can be caused by specifying an incorrect startup directory. 这必须是 migrate.exe 的位置This must be the location of migrate.exe
未经处理的异常: NullReferenceException:对象引用未设置为对象的实例。Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   在 System.web. Main (String [] 参数上为) at System.Data.Entity.Migrations.Console.Program.Main(String[] args)
这可能是由于未为你使用的方案指定必需参数引起的。This can be caused by not specifying a required parameter for a scenario that you are using. 例如,在不指定提供程序名称的情况下指定连接字符串。For example specifying a connection string without specifying the provider name.
错误:在程序集 "Classlibrary1.chainone" 中找到了多个迁移配置类型。ERROR: More than one migrations configuration type was found in the assembly 'ClassLibrary1'. 指定要使用的名称。Specify the name of the one to use. 作为错误状态,在给定的程序集中有多个配置类。As the error states, there is more than one configuration class in the given assembly. 必须使用/configurationType 开关来指定要使用的。You must use the /configurationType switch to specify which to use.
错误:无法加载文件或程序集 " < assemblyName > " 或其依赖项之一。ERROR: Could not load file or assembly ‘<assemblyName>’ or one of its dependencies. 给定的程序集名称或基本代码无效。The given assembly name or codebase was invalid. (HRESULT 中的异常: 0x80131047) (Exception from HRESULT: 0x80131047) 这可能是由于不正确地指定程序集名称或This can be caused by specifying an assembly name incorrectly or not having
错误:无法加载文件或程序集 " < assemblyName > " 或其依赖项之一。ERROR: Could not load file or assembly ‘<assemblyName>' or one of its dependencies. 试图加载的程序的格式不正确。An attempt was made to load a program with an incorrect format. 如果尝试对 x64 应用程序运行 migrate.exe,则会发生这种情况。This happens if you are trying to run migrate.exe against an x64 application. EF 5.0 和更低的将仅适用于 x86。EF 5.0 and below will only work on x86.