.Net Core WebAPi-SqlServer切换Mysql,实现CodeFirst
项目需要从SqlServer切换到Mysql,遇到了一些坑,作为记录
(1)Nuget安装
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.Relational
Microsoft.EntityFrameworkCore.Tools
Pomelo.EntityFrameworkCore.MySql
(2)修改连接字符串“appsettings.json”
1 "ConnectionStrings": { 2 //Sqlserver连接字符串 3 //"CoreFrameContext": "Server=.;DataBase=数据库名称;uid=sa;pwd=密码" 4 "CoreFrameContext": "Data Source=localhost;Database=名称;User ID=root;Password=密码;pooling=true;CharSet=utf8;port=3306;sslmode=none" 5 },
注:连接字符串的Data Source不能写成IP,否则会报错
(3)修改“Startup.cs”,找到ConfigureServices
1 //SqlServer 2 services.AddDbContext<NetCoreFrameDBContext>(opt => opt.UseSqlServer(Configuration.GetConnectionString("CoreFrameContext"))); 3 //Mysql 4 services.AddDbContext<NetCoreFrameDBContext>(opt => opt.UseMySQL(Configuration.GetConnectionString("CoreFrameContext"))); 5
(4)命令行迁移
打开“工具”-》“Nuget包管理器”-》“程序包管理器控制台”
Add-Migration 迁移名称
update-database