本文主要是介绍示例:EntityframeWorkCore应用MySql做数据迁移,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、目的:应用EF Core和MySql数据库了解做数据库迁移的步骤
二、环境:VS2019、.NetCore 2.2、Mysql 8.0.15、Win10
三、步骤:
1、新建类库(core)并创建DataContext
public class DataContext : DbContext{protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder){optionsBuilder.UseMySQL("server=localhost;userid=****;pwd=****;port=****;database=****;sslmode=none;");}public DbSet<City> Citys { get; set; } }public class City{public int Id { get; set; }public string Name { get; set; }public DateTime CDate { get; set; }}
2、安装Nuget包:MySql.Data.EntityFrameworkCore
3、安装Nuget包:MySql.Data.EntityFrameworkCore.Design
4、安装Nuget包:Microsoft.EntityFrameworkCore.Tools
5、在程序包管理控制台页面输入:get-help entityframework 查看所有可执行的命令
程序包管理器:工具->Nuget包管理系
Cmdlet Description
-------------------------- ---------------------------------------------------
Add-Migration Adds a new migration.
Drop-Database Drops the database.
Get-DbContext Gets information about a DbContext type.
Remove-Migration Removes the last migration.
Scaffold-DbContext Scaffolds a DbContext and entity types for a database.
Script-Migration Generates a SQL script from migrations.
Update-Database Updates the database to a specified migration.
6、执行命令一:add-migration +迁移名称
输出:没有报错,并且生成文件夹“Migrations”和如下文件,表示执行成功
public partial class init : Migration{protected override void Up(MigrationBuilder migrationBuilder){migrationBuilder.CreateTable(name: "Citys",columns: table => new{Id = table.Column<int>(nullable: false).Annotation("MySQL:AutoIncrement", true),Name = table.Column<string>(nullable: true)},constraints: table =>{table.PrimaryKey("PK_Citys", x => x.Id);});}protected override void Down(MigrationBuilder migrationBuilder){migrationBuilder.DropTable(name: "Citys");}}
7、执行命令二:update-datebase
输出:Done 并且数据库生成数据表或执行更改,表示执行成功
注:同时也可以应用命令Script-Migration可以生成数据库Sql脚本,在数据库执行
IF EXISTS(SELECT 1 FROM information_schema.tables WHERE table_name = '
__EFMigrationsHistory' AND table_schema = DATABASE())
BEGIN
CREATE TABLE `__EFMigrationsHistory` (`MigrationId` varchar(150) NOT NULL,`ProductVersion` varchar(32) NOT NULL,PRIMARY KEY (`MigrationId`)
);END;CREATE TABLE `Citys` (`Id` int NOT NULL AUTO_INCREMENT,`Name` text NULL,PRIMARY KEY (`Id`)
);INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
VALUES ('20190513100712_init', '2.2.4-servicing-10062');ALTER TABLE `Citys` ADD `CDate` datetime NOT NULL DEFAULT '0001-01-01 00:00:00.000000';INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
VALUES ('20190513101011_changecity', '2.2.4-servicing-10062');INSERT INTO `Citys` (`Id`, `CDate`, `Name`)
VALUES (1, '0001-01-01 00:00:00.000000', '成都');INSERT INTO `Citys` (`Id`, `CDate`, `Name`)
VALUES (5, '0001-01-01 00:00:00.000000', '北京');INSERT INTO `__EFMigrationsHistory` (`MigrationId`, `ProductVersion`)
VALUES ('20190513104003_changehasdata', '2.2.4-servicing-10062');
常见问题:
1、如果报“Table 'test.__efmigrationshistory' doesn't exist”表不存在错误,可以手动执行一次初始化sql语句
CREATE TABLE `__EFMigrationsHistory`
(
`MigrationId` nvarchar(150) NOT NULL,
`ProductVersion` nvarchar(32) NOT NULL,
PRIMARY KEY(`MigrationId`)
);
2、如果报“Value cannot be null. Parameter name: input ”则是少安装Nuget包,如:Microsoft.EntityFrameworkCore.Tools
以上就是EF Core 针对MySql数据库做迁移的方法和步骤
示例地址: https://github.com/HeBianGu/.NetCore-LearnDemo.git
这篇关于示例:EntityframeWorkCore应用MySql做数据迁移的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!