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

EF 6 Code-First系列文章目录:

在前面的一节中,你学习了自动迁移技术,当实体改变的时候,自动进行数据库迁移。这里你将会学习基于代码的数据库迁移技术。

基于代码的数据库迁移技术,在迁移的时候,提供了更多的控制。例如允许你配置添加额外的字符串,例如设置列的默认值,配置计算列等等。

为了使用基于代码的数据库迁移,你需要在程序包管理控制台中输入:

  1. Enable-Migrations:在项目中启用数据库迁移,然后会创建一个Configuration类
  2. Add-Migration:创建了一个迁移类,其中指定了Up和Down方法。
  3. Update-Database:执行Add_migration指令中创建的迁移,将改变应用到数据库中。

为了使用基于代码的数据库迁移,首先在程序包管理控制台中执行enable-migrations命令。

Enable-Migrations指令会创建Configuration类,这个Configuration类继承自DbMigrationsConfiguration ,Configuration类中包含这句代码:AutomaticMigrationsEnabled = false.

现在你需要在上下文类中设置数据库初始化策略为MigrateDatabaseToLatestVersion :

public class SchoolContext: DbContext
{
public SchoolDBContext(): base("SchoolDB")
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<SchoolDBContext, EF6Console.Migrations.Configuration>());
} public DbSet<Student> Students { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{ }
}

现在使用Add-Migration命令创建一个迁移类 ,后面跟着迁移类的名称:

上面的命令将会创建一个时间戳_SchoolDB-v1.cs文件,类里面包含Up和Down方法:

正如你所见,Up方法包含创建数据库对象的代码,并且Down方法包含删除数据库的代码。你同样可以编写代码,进行额外的配置。这就是优于自动迁移的地方。

为了了解更多add-migrations命令参数,你可以执行get-help add-migration或者get-help add-migration -detailed:

PM> get-help add-migration

NAME
Add-Migration SYNOPSIS
Scaffolds a migration script for any pending model changes. SYNTAX
Add-Migration [-Name] <String> [-Force] [-ProjectName <String>] [-StartUpProjectName <String>]
[-ConfigurationTypeName <String>] [-ConnectionStringName <String>] [-IgnoreChanges]
[-AppDomainBaseDirectory <String>] [<CommonParameters>] Add-Migration [-Name] <String> [-Force] [-ProjectName <String>] [-StartUpProjectName <String>]
[-ConfigurationTypeName <String>] -ConnectionString <String> -ConnectionProviderName <String>
[-IgnoreChanges] [-AppDomainBaseDirectory <String>] [<CommonParameters>] DESCRIPTION
Scaffolds a new migration script and adds it to the project. RELATED LINKS 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".

在使用Add-Migration命令之后,你需要更新数据库。通过执行Update-Database命令,来提交修改到数据库中,还可以在后面加上–verbose 就可以看到生成的SQL脚本:

执行get-help update-database或者get-help update-database -detailed命令:

PM> get-help update-database

NAME
Update-Database SYNOPSIS
Applies any pending migrations to the database. SYNTAX
Update-Database [-SourceMigration <String>] [-TargetMigration <String>] [-Script] [-Force]
[-ProjectName <String>] [-StartUpProjectName <String>] [-ConfigurationTypeName <String>]
[-ConnectionStringName <String>] [-AppDomainBaseDirectory <String>] [<CommonParameters>] Update-Database [-SourceMigration <String>] [-TargetMigration <String>] [-Script] [-Force]
[-ProjectName <String>] [-StartUpProjectName <String>] [-ConfigurationTypeName <String>]
-ConnectionString <String> -ConnectionProviderName <String> [-AppDomainBaseDirectory <String>]
[<CommonParameters>] DESCRIPTION
Updates the database to the current model by applying pending migrations. RELATED LINKS 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".

到这个时候,数据库就被创建或更新了,现在不管什么时候,模型发生改变的时候,执行Add-Migration 带上参数名,就创建一个新的迁移文件,然后执行Update-Database命令,就将修改提交到数据库了。

迁移回退

假设你想要回退到之前的任何一个状态,那么你可以执行update-database后面跟着–TargetMigration,指定你想要回退的版本。例如,假设SchoolDB数据库有很多迁移记录,但是你想回退到第一个版本,那么你可以执行下面的代码:

PM> update-database -TargetMigration:SchoolDB-v1

20.2.翻译系列:EF 6中基于代码的数据库迁移技术【EF 6 Code-First系列】的更多相关文章

  1. 20.翻译系列:Code-First中的数据库迁移技术【EF 6 Code-First系列】

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

  2. 12.翻译系列:EF 6 中配置一对多的关系【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-f ...

  3. Entity Framework入门教程(18)---EF6中基于代码进行配置方式

    EF6中基于代码进行配置方式 我们以前对EF进行配置时是在app.config/web.config下的<entityframework>节点下进行配置的,EF6引进了基于代码的配置方法. ...

  4. 2.EF中 Code-First 方式的数据库迁移

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/code-first-migrations-with-entity-framework/ 系列目 ...

  5. EF中 Code-First 方式的数据库迁移

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/code-first-migrations-with-entity-framework/ 系列目 ...

  6. 2017.11.12 web中JDBC 方式访问数据库的技术

    JavaWeb------ 第四章 JDBC数据库访问技术 在JavaWeb应用程序中数据库访问是通过Java数据库连接(JavaDateBase Connectivity简称JDBC)数据库的链接一 ...

  7. [更新中]【South使用总结】django开发中使用South进行数据库迁移

    Django开发中使用South进行数据库迁移的使用总结 South的详细资料可产看官方文档http://south.readthedocs.org/en/latest South安装配置 pip i ...

  8. Android开发中使用代码删除数据库

    更多信息参考:Android开发中使用代码删除数据库 在Android开发中,如果用到数据库,就会有一个很麻烦的问题,就是有时候需要删除数据库很麻烦,要打开Android Device Monitor ...

  9. EF Core 中多次从数据库查询实体数据,DbContext跟踪实体的情况

    使用EF Core时,如果多次从数据库中查询一个表的同一行数据,DbContext中跟踪(track)的实体到底有几个呢?我们下面就分情况讨论下. 数据库 首先我们的数据库中有一个Person表,其建 ...

随机推荐

  1. Running Median POJ - 3784 (对顶堆/优先队列 | 链表)

    For this problem, you will write a program that reads in a sequence of 32-bit signed integers. After ...

  2. POJ 1195 Mobile phones【二维树状数组】

    <题目链接> 题目大意: 一个由数字构成的大矩阵,开始是全0,能进行两种操作1) 对矩阵里的某个数加上一个整数(可正可负)2) 查询某个子矩阵里所有数字的和要求对每次查询,输出结果 解题分 ...

  3. Oracle 在字符串中输入单引号或特殊字符

    -- Start 字符串是用单引号括起来的,如果想在字符串中输入单引号该怎么办呢?有两种方法. 方法一:是用两个单引号代表一个单引号 SELECT 'I''m Shangbo' FROM DUAL; ...

  4. Cursor for loop in Oracle

    declare l_sql ); -- variable that contains a query l_c sys_refcursor; -- cursor variable(weak cursor ...

  5. linux学习笔记 apache php mysql +linux

    1 #yum remove httpd 2 #yum -y install httpd php-common php-devel php-gd php-mcrypt php-mbstring php- ...

  6. BZOJ.5251.[八省联考2018]劈配mentor(最大流)

    BZOJ 洛谷 对于每个人,每次枚举一个志愿看是否能增广即可. 对于第二问,可以保留第一问中\(n\)次增广前后的\(n\)张图,二分,在对应图上看是否能增广即可. 貌似匈牙利的某种写法比网络流优多了 ...

  7. Scrapy基础(五) ------css选择器基础

    基本语法: *                  选择所有节点#container         选择id为container的节点.container      选择所有class包含contai ...

  8. curl获取结果乱码的解决方法之CURLOPT_ENCODING(curl/Post请求)

    //php脚本开始   /*POST请求远程内容函数*/   function ppost($url,$data,$ref){ // 模拟提交数据函数       $curl = curl_init( ...

  9. 潭州课堂25班:Ph201805201 tornado 项目 第二课 项目 基本功能模块和 Git 使用 (课堂笔记)

    tornado 相关说明 把图片显示在页面, 创建个 static 文件夹, 在这个文件下存放几张图片 在配置中指定静态文件路径, 在 html 文件中迭代出图片, 创建个包,重构 handlers ...

  10. python网络编程(三)

    udp网络通信过程 udp应用:echo服务器 参考代码 #coding=utf-8 from socket import * #1. 创建套接字 udpSocket = socket(AF_INET ...