Entity Framework Migrations are handled from the package manager console in Visual Studio. The usage is shown in various tutorials, but I haven’t found a complete list of the commands available and their usage, so I created my own. There are four available commands.

The information here is the output of running get-help command-name -detailed for each of the commands in the package manager console (running EF 4.3.1). I’ve also added some own comments where I think some information is missing. My own comments are placed under the Additional Information heading.

Please note that all commands should be entered on the same line. I’ve added line breaks to avoid horizontal scrollbars.

Enable-Migrations

Enables Code First Migrations in a project.

Syntax

Enable-Migrations [-EnableAutomaticMigrations] [[-ProjectName] <String>]
[-Force] [<CommonParameters>]

Description

Enables Migrations by scaffolding a migrations configuration class in the project. If the  target database was created by an initializer, an initial migration will be created (unless automatic migrations are enabled via the EnableAutomaticMigrations parameter).

Parameters

-ENABLEAUTOMATICMIGRATIONS

Specifies whether automatic migrations will be enabled in the scaffolded migrations configuration. If ommitted, automatic migrations will be disabled.

-PROJECTNAME <STRING>

Specifies the project that the scaffolded migrations configuration class will be added to. If omitted, the default project selected in package manager console is used.

-FORCE

Specifies that the migrations configuration be overwritten when running more than once for given project.

<COMMONPARAMETERS>

This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer and OutVariable. For more information, type: get-help about_commonparameters.

Remarks

To see the examples, type: get-help Enable-Migrations -examples.
For more information, type: get-help Enable-Migrations -detailed.
For technical information, type: get-help Enable-Migrations -full.

Additional Information

The flag for enabling automatic migrations is saved in the Migrations\Configuration.cs file, in the constructor. To later change the option, just change the assignment in the file.

public Configuration()
{
AutomaticMigrationsEnabled = false;
}

Add-Migration

Scaffolds a migration script for any pending model changes.

Syntax

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

Description

Scaffolds a new migration script and adds it to the project.

Parameters

-NAME <STRING>

Specifies the name of the custom script.

-FORCE

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

-PROJECTNAME <STRING>

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

-STARTUPPROJECTNAME <STRING>

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

-CONFIGURATIONTYPENAME <STRING>

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

-CONNECTIONSTRINGNAME <STRING>

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

-CONNECTIONSTRING <STRING>

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

-CONNECTIONPROVIDERNAME <STRING>

Specifies the provider invariant name of the connection string.

-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.

<COMMONPARAMETERS>

This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer and OutVariable. For more information, type: get-help about_commonparameters.

Remarks

To see the examples, type: get-help Add-Migration -examples.
For more information, type: get-help Add-Migration -detailed.
For technical information, type: get-help Add-Migration -full.

Update-Database

Applies any pending migrations to the database.

Syntax

Update-Database [-SourceMigration <String>]
[-TargetMigration <String>] [-Script] [-Force] [-ProjectName <String>]
[-StartUpProjectName <String>] [-ConfigurationTypeName <String>]
[-ConnectionStringName <String>] [<CommonParameters>]
 
Update-Database [-SourceMigration <String>] [-TargetMigration <String>]
[-Script] [-Force] [-ProjectName <String>] [-StartUpProjectName <String>]
[-ConfigurationTypeName <String>] -ConnectionString <String>
-ConnectionProviderName <String> [<CommonParameters>]

Description

Updates the database to the current model by applying pending migrations.

Parameters

-SOURCEMIGRATION <STRING>

Only valid with -Script. Specifies the name of a particular migration to use as the update’s starting point. If ommitted, the last applied migration in the database will be used.

-TARGETMIGRATION <STRING>

Specifies the name of a particular migration to update the database to. If ommitted, the current model will be used.

-SCRIPT

Generate a SQL script rather than executing the pending changes directly.

-FORCE

Specifies that data loss is acceptable during automatic migration of the
database.

-PROJECTNAME <STRING>

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

-STARTUPPROJECTNAME <STRING>

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

-CONFIGURATIONTYPENAME <STRING>

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

-CONNECTIONSTRINGNAME <STRING>

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

-CONNECTIONSTRING <STRING>

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

-CONNECTIONPROVIDERNAME <STRING>

Specifies the provider invariant name of the connection string.

<COMMONPARAMETERS>

This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer and OutVariable. For more information, type: get-help about_commonparameters.

Remarks

To see the examples, type: get-help Update-Database -examples.
For more information, type: get-help Update-Database -detailed.
For technical information, type: get-help Update-Database -full.

Additional Information

The command always runs any pending code-based migrations first. If the database is still incompatible with the model the additional changes required are applied as an separate automatic migration step if automatic migrations are enabled. If automatic migrations are disabled an error message is shown.

Get-Migrations

Displays the migrations that have been applied to the target database.

Syntax

Get-Migrations [-ProjectName <String>] [-StartUpProjectName <String>]
[-ConfigurationTypeName <String>] [-ConnectionStringName <String>]
[<CommonParameters>]
 
Get-Migrations [-ProjectName <String>] [-StartUpProjectName <String>]
[-ConfigurationTypeName <String>] -ConnectionString <String>
-ConnectionProviderName <String> [<CommonParameters>]

Description

Displays the migrations that have been applied to the target database.

Parameters

-PROJECTNAME <STRING>

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

-STARTUPPROJECTNAME <STRING>

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

-CONFIGURATIONTYPENAME <STRING>

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

-CONNECTIONSTRINGNAME <STRING>

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

-CONNECTIONSTRING <STRING>

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

-CONNECTIONPROVIDERNAME <STRING>

Specifies the provider invariant name of the connection string.

<COMMONPARAMETERS>

This cmdlet supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction, WarningVariable, OutBuffer and OutVariable. For more information, type: get-help about_commonparameters.

Remarks

To see the examples, type: get-help Get-Migrations -examples.
For more information, type: get-help Get-Migrations -detailed.
For technical information, type: get-help Get-Migrations -full.

Additional Information

The powershell commands are complex powershell functions, located in the tools\EntityFramework.psm1file of the Entity Framework installation. The powershell code is mostly a wrapper around theSystem.Data.Entity.Migrations.MigrationsCommands found in thetools\EntityFramework\EntityFramework.PowerShell.dll file. First a MigrationsCommands object is instantiated with all configuration parameters. Then there is a public method on the MigrationsCommandsobject for each of the available commands.

EntityFramework:EF Migrations Command Reference的更多相关文章

  1. ORM框架:EF与NHibernate了解

    在.Net Framework SP1微软包含一个实体框架(Entity Framework),此框架可以理解成微软的一个ORM产品.用于支持开发人员通过对概念性应用程序模型编程(而不是直接对关系存储 ...

  2. MVC5 + EF6 完整入门教程三:EF来了

    期待已久的EF终于来了 学完本篇文章,你将会掌握基于EF数据模型的完整开发流程. 本次将会完成EF数据模型的搭建和使用. 基于这个模型,将之前的示例添加数据库查询验证功能. 文章提纲 概述 & ...

  3. 译 - 第 1 章:EF入门

    章节信息 Entity Framework 6 Recipes 第二版第一章: Chapter 1: Getting Started with Entity Framework ----------- ...

  4. 20.2.翻译系列:EF 6中基于代码的数据库迁移技术【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/code-based-migration-in-code-first.aspx EF 6 ...

  5. 20.1翻译系列:EF 6中自动数据迁移技术【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/automated-migration-in-code-first.aspx EF 6 ...

  6. 15.翻译系列:EF 6中的级联删除【EF 6 Code-First 系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/cascade-delete-in-code-first.aspx EF 6 Code- ...

  7. 6.翻译系列:EF 6 Code-First中数据库初始化策略(EF 6 Code-First系列)

    原文链接:http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-firs ...

  8. .net core执行dotnet ef migrations createmodel等命令出错

    .net core执行dotnet ef migrations createmodel等命令出错 执行dotnet ef migrations createmodel.dotnet ef migrat ...

  9. 任务44:Identity MVC: EF + Identity实现

    使用VSCode开发 Razer的智能感知不好.所以这里切换为VS2017进行开发: 新建一个Data的文件夹来存放我们的DBContext.在Data文件夹下新建: ApplicationDbCon ...

随机推荐

  1. html学习-css

    1.css初识 css 中文解释:层叠样式表,把html比作骨骼的话,css就是衣服,他的外在都能通过css来修饰,js则是肌肉,能使html动起来.产生用户交互... 1.1css样式表类型 css ...

  2. Jmeter----连接mysql数据库及常见问题处理

    jmeter要链接mysql数据库,首先得下载mysql jdbc驱动包,(注:驱动包的版本一定要与你数据库的版本匹配,驱动版本低于mysql版本有可能会导致连接失败报错) 本人的数据为5.7.18, ...

  3. TPC-H数据导入MySQL教程

    0. TPC-H是啥 TPC-H是TPC提供的一个benchmark,用来模拟一个现实中的商业应用,可以生成一堆虚构的数据,且自带一些查询,可以导入到各种数据库中来模拟现实需求,检查性能. 具体是怎样 ...

  4. MIAC HW2

    MIAC的第二次作业,翻了一些fashion网站找了点灵感,重新设计了一下UI. 因为给的html里有nav之类的HTML5新特性,所以索性就不管IE的兼容了.chrome下的效果: FF下也差不多. ...

  5. virtualenv python的虚拟环境

    官网:https://virtualenv.pypa.io/en/stable/userguide/ virtualenv通过创建独立Python开发环境的工具, 来解决依赖.版本问题 基本使用: d ...

  6. c++ 容器学习 理论

    [转载]http://blog.csdn.net/acosoft/article/details/4395468 在面向对象的语言中,大多引入了容器的概念.那么 什么 是 容器?实质上就是一组相同类型 ...

  7. jquery validate表单验证插件的基本使用方法及功能拓展

    1 表单验证的准备工作 在开启长篇大论之前,首先将表单验证的效果展示给大家.    1.点击表单项,显示帮助提示 2.鼠标离开表单项时,开始校验元素 3.鼠标离开后的正确.错误提示及鼠标移入时的帮助提 ...

  8. LeetCode 260. Single Number III(只出现一次的数字 III)

    LeetCode 260. Single Number III(只出现一次的数字 III)

  9. CSS3 之媒体查询Media Query

    Media Queries是CSS3有关媒体查询的属性,有了CSS3 之媒体查询Media Queries就可以进行媒体查询,针对每个不同的媒体进行不同的样式编写.传说中的Web响应式布局就可以毫无压 ...

  10. ubuntu调错

    最近运行一个程序,出现错误 ’event.h‘,猜想是缺少event lib 库 于是安装sudo apt-get install libevent-dev 即可.