原文链接: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. The path is not a valid path to the xxx kernel header

    在安装vmtools时无意中出现了这样的问题 1.gcc错误 Searching for GCC- The path "" is not valid path to the gcc ...

  2. Codeforces 633C Spy Syndrome 2 【Trie树】+【DFS】

    <题目链接> 题目大意:给定一个只有小写字母组成的目标串和m个模式串(里面可能有大写字母),记目标串反过来后的串为S,让你从m个模式串中选出若干个组成S串(不区分大小写).输出任意一种方案 ...

  3. [ 低危 ] mt网CRLF

    漏洞: xxx.meituan.com/%0d%0aevilheadername:%20inject_by_whoamisb 原理猜测: 收到xxx二级域名的时候,会location跳转到该域名(这是 ...

  4. String、StringBuffer、StringBuilder的比较

    看String类的定义:public final class String...{private final char value[];} 看AbstractStringBuilder类的定义:abs ...

  5. Halcon 常用算子使用场合

    Chapter 1 :Classification 1.1 Gaussian-Mixture-Models 1.add_sample_class_gmm 功能:把一个训练样本添加到一个高斯混合模型的训 ...

  6. HQL实用技术

    HQL是Hibernate Query Language的缩写,提供更加丰富灵活.更为强大的查询能力:HQL更接近SQL语句查询语法. HQL基础查询  1.获取部分列 多列 /** * 获取部分列 ...

  7. error :expected initializer before

    很可能头文件或者前面的某个定义少了个:

  8. [P2058][NOIP2015]海港 (模拟)

    %%%ADMAN #include<cstdio> using namespace std; int n,tot,now,ans,h; ],k[],a[],sum[]; int main( ...

  9. 使用Three.js为QQ用户生成3D头像阵列

    东西其实比较简单,就是输出某一范围内QQ账号的3D头像 涉及的技术主要是Three.js的基本使用 而后通过腾讯的接口异步并发请求QQ用户头像,Canavs作图生成Texture应用在球体上 需要注意 ...

  10. 获取html下的所有纯文本的方法

    第一种是看别人博客的,第二种是自己发现的. 第一种: #-*- coding: utf8 -*- import re html = """ <div class=& ...