ABP中使用MySql数据库

一:卸载Sql Server的Nuget包

打开程序包管理器控制台(工具→Nuget 包管理器→程序包管理器控制台),修改默认项目为.EntityFrameworkCore

然后依次执行: 

uninstall-package microsoft.entityframeworkcore.design
uninstall-package microsoft.entityframeworkcore.sqlserver

二:安装Mysql的Nuget包

右键点击.EntityFrameworkCore项目选择管理Nuget程序包,浏览搜索 Pomelo.EntityFrameworkCor,安装microsoft.entityframeworkcore.MySql和microsoft.entityframeworkcore.design 

注意根据core版本选择

 三:更改DbContextConfigurer文件

using System;
using System.Data.Common;
using Microsoft.EntityFrameworkCore;

namespace MyProduct.EntityFrameworkCore
{
    public static class MyProductDbContextConfigurer
    {
        public static void Configure(DbContextOptionsBuilder<MyProductDbContext> builder, string connectionString)
        {
            //原来的
            //builder.UseSqlServer(connectionString);
            //修改后
            builder.UseMySql(connectionString, new MySqlServerVersion(new Version(8, 0, 11)));
        }

        public static void Configure(DbContextOptionsBuilder<MyProductDbContext> builder, DbConnection connection)
        {
            //原来的
            //builder.UseSqlServer(connection);
            //修改后
            builder.UseMySql(connection, new MySqlServerVersion(new Version(8, 0, 11)));
        }
    }
}

 四.修改数据库连接字符串

 1.修改.Migrator项目里面的appsettings.json链接字符串

{
  "ConnectionStrings": {
    //Trusted_Connection=True切换成mysql要去掉,不然报错未授权
    //"Default": "Server=localhost; Database=DouhuiSmartDb; Trusted_Connection=True;"
    "Default": "Server=localhost;Port=3306; Database=数据库名称;Uid=数据库登录名(默认root);Pwd=密码;"
  }
}

2.修改.Host项目里面的appsettings.json链接字符串

{
  "ConnectionStrings": {
    //Trusted_Connection=True切换成mysql要去掉,不然报错未授权
    //"Default": "Server=localhost; Database=DouhuiSmartDb; Trusted_Connection=True;"
    "Default": "Server=localhost;Port=3306; Database=数据库名称;Uid=数据库登录名(默认root);Pwd=密码;"
  }
}

五:执行add-migration命令

在执行 add-migration时要先删除.EntityFrameworkCore下面Migrations文件夹里面的所有东西

注:查询了很多文档,如有侵权,请留言删除

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>