EF对SqlServer的支持很好,这个不说了。

Mysql也可以使用相关的功能,本篇文章主要介绍一下,几个常用的命令 Add-Migration 、 Update-DataBase 、 Script-Migration

 

1首先需要使用到三个包

Microsoft.EntityFrameworkCore.Tools

Pomelo.EntityFrameworkCore.MySql

Pomelo.EntityFrameworkCore.MySql.Design

 

2 其次要注意下面几项

① 启动项目 需要能正常编译通过

②DBContext中的连接字符串可以使用,能正常连接到数据库,并且具有读写权限。没有写后面在Update的时候失败。

③数据库应该要先创建一下,表可以不创建,后面可自动更新进去

 

3.最后打开 VS中的程序包管理器,选择DBContext 所在的项目

EF framework 连接mysql mysql efcore_数据库

4.添加迁移文件

使用  

Add-Migration (你的迁移文件名称)

例如:执行之后

PM> Add-Migration init20191220
To undo this action, use Remove-Migration.

然后会看到出现了一个 Migration文件夹,然后里面多了一个类

EF framework 连接mysql mysql efcore_文件名_02

5.使用 Update-DataBase

此条的前提是,账号具有写权限,执行之后会直接在数据库中生成对应的表。

例如:

PM> Update-Database
Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='tcycollgamemanagedb' AND TABLE_NAME='__EFMigrationsHistory';
Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE `__EFMigrationsHistory` (
    `MigrationId` varchar(95) NOT NULL,
    `ProductVersion` varchar(32) NOT NULL,
    CONSTRAINT `PK___EFMigrationsHistory` PRIMARY KEY (`MigrationId`)
);
Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='tcycollgamemanagedb' AND TABLE_NAME='__EFMigrationsHistory';
Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT `MigrationId`, `ProductVersion`
FROM `__EFMigrationsHistory`
ORDER BY `MigrationId`;
Applying migration '20191220015841_init20191220'.
Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE `GameInfos` (
    `Id` int NOT NULL AUTO_INCREMENT,
    `GameId` bigint NOT NULL,
    `GameCode` varchar(10) CHARACTER SET utf8mb4 NOT NULL,
    `GameType` int NOT NULL,
    CONSTRAINT `PK_GameInfos` PRIMARY KEY (`Id`)
);
Executing DbCommand [Parameters=[], CommandType='Text', CommandTimeout='30']
INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
VALUES ('20191220015841_init20191220', '3.0.0');
Done.

 

EF framework 连接mysql mysql efcore_Text_03

 

 

当我们修改类的字段、增加删除、增加索引等操作的时候,可以再 Add一个Migration,然后再Update-DataBase。

 

6.如果没有写权限怎么办,上面的操作可以通过生成脚本的方式。使用Script-Migration这个命令

PM> Script-Migration

执行之后上面的会生成下面的SQL

CREATE TABLE IF NOT EXISTS `__EFMigrationsHistory` (
    `MigrationId` varchar(95) NOT NULL,
    `ProductVersion` varchar(32) NOT NULL,
    CONSTRAINT `PK___EFMigrationsHistory` PRIMARY KEY (`MigrationId`)
);

CREATE TABLE `GameInfos` (
    `Id` int NOT NULL AUTO_INCREMENT,
    `GameId` bigint NOT NULL,
    `GameCode` varchar(10) CHARACTER SET utf8mb4 NOT NULL,
    `GameType` int NOT NULL,
    CONSTRAINT `PK_GameInfos` PRIMARY KEY (`Id`)
);

INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
VALUES ('20191220015841_init20191220', '3.0.0');

然后在手动执行一下也可以。

重要的记录一下几个常用的参数:官方记录如下

The from migration should be the last migration applied to the database before running the script. If no migrations have been applied, specify 0 (this is the default).

The to migration is the last migration that will be applied to the database after running the script. This defaults to the last migration in your project.

from迁移应该是运行脚本之前应用于数据库的最后一次迁移。如果没有应用迁移,请指定0(这是默认设置)。

to迁移是运行脚本后将应用于数据库的最后一次迁移。这默认为项目中的最后一次迁移。 

举个例子

例如修改了上面的数据库实体类,又增加了一个Migration。

EF framework 连接mysql mysql efcore_文件名_04

 

 此时如果不指定From和To,直接使用命令Script-Migration,生成的脚本如下,我们新增的GamePlay没有啊。

EF framework 连接mysql mysql efcore_Text_05

 

 如果加上From 第一次已经执行过的脚本,生成的脚本如下:

Script-Migration -From 20191220015841_init20191220

EF framework 连接mysql mysql efcore_Text_06

所以第一次我们可以什么都不加,以后的每次都使用上一次生成的名称。

此外可以通过下面查看具体的其他的参数

get-help Add-Migration -full

get-help Script-Migration -full

get-help Update-DataBase -full