.NET 6使用Migartion进行数据库迁移
【代码】.NET 6使用Migartion进行数据库迁移。
·
Nuget包
Microsoft.EntityFrameworkCore.Tools
使用流程
打开VS中的程序包管理器,选择DBContext 所在的项目

添加迁移文件
Add-Migration (你的迁移文件名称)
执行后:
PM> Add-Migration Add_TestConsole12_Table
Build started...
Build succeeded.
The Entity Framework tools version '6.0.2' is older than that of the runtime '7.0.7'. Update the tools for the latest features and bug fixes. See https://aka.ms/AAc1fbw for more information.
To undo this action, use Remove-Migration.
出现了一个 Migration文件夹,然后里面多了迁移生成的类,以及数据快照

一、使用 Update-DataBase
一定要在DbContext加DBSet,才能和数据库有联系,且账号具有写权限,执行之后会直接在数据库中生成对应的表
执行后:
PM> Update-Database
Build started...
Build succeeded.
The Entity Framework tools version '6.0.2' is older than that of the runtime '7.0.7'. Update the tools for the latest features and bug fixes. See https://aka.ms/AAc1fbw for more information.
Applying migration '20231016102620_StudentInit'.
Done.
数据库中会新建一个Migration记录表,名为__efmigrationshistory,每次update时会自动记录到表中:

二、生成脚本方式
PM> Script-Migration
Build started...
Build succeeded.
The Entity Framework tools version '6.0.2' is older than that of the runtime '7.0.7'. Update the tools for the latest features and bug fixes. See https://aka.ms/AAc1fbw for more information.
CREATE TABLE IF NOT EXISTS `__EFMigrationsHistory` (
`MigrationId` varchar(150) CHARACTER SET utf8mb4 NOT NULL,
`ProductVersion` varchar(32) CHARACTER SET utf8mb4 NOT NULL,
CONSTRAINT `PK___EFMigrationsHistory` PRIMARY KEY (`MigrationId`)
) CHARACTER SET=utf8mb4;
START TRANSACTION;
ALTER DATABASE CHARACTER SET utf8mb4;
CREATE TABLE `Student` (
`Id` char(36) COLLATE ascii_general_ci NOT NULL,
`Name` longtext CHARACTER SET utf8mb4 NOT NULL,
`Sex` int NOT NULL,
`Age` int NOT NULL,
`CreatedAt` datetime(6) NOT NULL,
`IsDeleted` tinyint(1) NOT NULL,
CONSTRAINT `PK_Student` PRIMARY KEY (`Id`)
) CHARACTER SET=utf8mb4;
CREATE TABLE `Test` (
`Id` char(36) COLLATE ascii_general_ci NOT NULL,
`Name` longtext CHARACTER SET utf8mb4 NOT NULL,
`CreatedAt` datetime(6) NOT NULL,
`IsDeleted` tinyint(1) NOT NULL,
CONSTRAINT `PK_Test` PRIMARY KEY (`Id`)
) CHARACTER SET=utf8mb4;
INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
VALUES ('20231016102620_StudentInit', '7.0.7');
COMMIT;
START TRANSACTION;
CREATE TABLE `TestConsole` (
`Id` char(36) COLLATE ascii_general_ci NOT NULL,
`Name` longtext CHARACTER SET utf8mb4 NOT NULL,
`CreatedAt` datetime(6) NOT NULL,
`IsDeleted` tinyint(1) NOT NULL,
CONSTRAINT `PK_TestConsole` PRIMARY KEY (`Id`)
) CHARACTER SET=utf8mb4;
INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
VALUES ('20231017013902_Add_TestConsole_Table', '7.0.7');
COMMIT;
START TRANSACTION;
INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
VALUES ('20231017014640_Add_TestConsole12_Table', '7.0.7');
COMMIT;
常用命令
Add-Migration 【版本名称】
//指定DbContext创建迁移
Add-Migration 【版本名称】 -Context 【TestDbContext】
//默认执行最新的脚本
Update-Database
//指定版本名称
Update-Database 【版本名称】
//指定DbContext
Update-Database 【版本名称】 -Context 【TestDbContext】
//默认删除 最后一次迁移
Remove-Migration
//指定DbContext
Remove-Migration 【版本名称】 -Context 【TestDbContext】
//生成迁移SQL代码
Script-Migration
//生成版本D到版本F的SQL脚本
Script-Migration D F
//生成版本D到最新版本的SQL脚本
Script-Migration D
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐



所有评论(0)