EntityFramework:EF Migrations Command Reference
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.
- Enable-Migrations: Enables Code First Migrations in a project.
- Add-Migration: Scaffolds a migration script for any pending model changes.
- Update-Database: Applies any pending migrations to the database.
- Get-Migrations: Displays the migrations that have been applied to the target database.
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>] |
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() |
Add-Migration
Scaffolds a migration script for any pending model changes.
Syntax
Add-Migration [-Name] <String> [-Force] |
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>] |
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>] |
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的更多相关文章
- ORM框架:EF与NHibernate了解
在.Net Framework SP1微软包含一个实体框架(Entity Framework),此框架可以理解成微软的一个ORM产品.用于支持开发人员通过对概念性应用程序模型编程(而不是直接对关系存储 ...
- MVC5 + EF6 完整入门教程三:EF来了
期待已久的EF终于来了 学完本篇文章,你将会掌握基于EF数据模型的完整开发流程. 本次将会完成EF数据模型的搭建和使用. 基于这个模型,将之前的示例添加数据库查询验证功能. 文章提纲 概述 & ...
- 译 - 第 1 章:EF入门
章节信息 Entity Framework 6 Recipes 第二版第一章: Chapter 1: Getting Started with Entity Framework ----------- ...
- 20.2.翻译系列:EF 6中基于代码的数据库迁移技术【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/code-based-migration-in-code-first.aspx EF 6 ...
- 20.1翻译系列:EF 6中自动数据迁移技术【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/automated-migration-in-code-first.aspx EF 6 ...
- 15.翻译系列:EF 6中的级联删除【EF 6 Code-First 系列】
原文链接:https://www.entityframeworktutorial.net/code-first/cascade-delete-in-code-first.aspx EF 6 Code- ...
- 6.翻译系列:EF 6 Code-First中数据库初始化策略(EF 6 Code-First系列)
原文链接:http://www.entityframeworktutorial.net/code-first/database-initialization-strategy-in-code-firs ...
- .net core执行dotnet ef migrations createmodel等命令出错
.net core执行dotnet ef migrations createmodel等命令出错 执行dotnet ef migrations createmodel.dotnet ef migrat ...
- 任务44:Identity MVC: EF + Identity实现
使用VSCode开发 Razer的智能感知不好.所以这里切换为VS2017进行开发: 新建一个Data的文件夹来存放我们的DBContext.在Data文件夹下新建: ApplicationDbCon ...
随机推荐
- 转- 阿里云、Amazon、Google云数据库方案架构与技术分析
「一切都会运行在云端」. 云时代早已来临,本文着眼于顶级云服务商云服务商的云数据库方案背后的架构,以及笔者最近观察到的一些对于云数据库有意义的工业界的相关技术的进展,希望读者能有所收获. 现在越来越多 ...
- 如何设计一个基于Node.js和Express的网站架构?
前言 今年七月份,我和几个小伙伴们合伙建立了一个开发团队.业务开展如火如荼的同时,团队宣传就提上了日程,所以迫切需要搭建公司网站出来.确定目标后我们就开始考虑如果构建一个企业网站.先是进行业内调查,看 ...
- 《java虚拟机》----虚拟机字节码执行引擎
No1: 物理机的执行引擎是直接建立在处理器.硬件.指令集合操作系统层面上的,而虚拟机的执行引擎则是由自己实现的,因此可以自行制定指令集与执行引擎的结构体系,并且能够执行那些不被硬件直接支持的指令集格 ...
- 力扣:丑数II和数组中前K大的元素
数组中的第K个元素 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k ...
- ubuntu 远程登录(ssh)
Ubuntu下通过SSH远程登录服务器的方法 首先在服务器上安装ssh的服务器端. $ sudo aptitude install openssh-server 启动ssh-server. $ /et ...
- 51nod1981 如何愉快地与STL玩耍
先摆官方题解吧......... ....................有什么好讲的呢....... 注意一些地方常数优化一下.......然后......$bitset$怎么暴力怎么来吧..... ...
- hdu 4790 Just Random
思路:对于a<=x<=b,c<=y<=d,满足条件的结果为ans=f(b,d)-f(b,c-1)-f(a-1,d)+f(a-1,c-1). 而函数f(a,b)是计算0<= ...
- Problem G: 切煎饼
Description 王小二自夸刀工不错,有人放一张大的圆煎饼在砧板上,问他:饼不允许离开砧板,切100刀最多能切多少块? Input 多组测试数据,每组输入1个整数,代表切的刀数 Output 每 ...
- poj 2104 静态主席树
我的第一道主席树(静态). 先记下自己对主席树的理解: 主席树的作用是用于查询区间第k大的元素(初始化nlog(n),查询log(n)) 主席树=可持续线段树+前缀和思想 主席树实际上是n棵线段树(由 ...
- JDK源码(1.7) -- java.util.AbstractList<E>
java.util.AbstractList<E> 源码分析(JDK1.7) ------------------------------------------------------- ...