ABP vnext中使用MySQL数据库

1、开发环境

开发工具:Vs2019
运行时:dotnet core 3.1
数据库:MySql 8.0
abp版本:2.7.0

2、修改数据库连接字符串

 "Default": "Server=127.0.0.1;port=3306;Database=数据库;Uid=账户;Pwd=密码;CharSet=utf8;Allow User Variables=True;"

3 、移除SQL Server 并安装MySQL的Nuget包

打开程序包管理器控制台,并选中.EntityFrameworkCore
在这里插入图片描述

uninstall-package Volo.Abp.EntityFrameworkCore.SqlServer;

install-package Volo.Abp.EntityFrameworkCore.SqlServer;

4、项目中相关SQL Server 切换MySql

在解决方案的EntityFrameCore层找到BookStoreEntityFrameworkCoreModule文件,并做如下修改:
options.UseSQLServer() 修改为:options.UseMySQL()。
将依赖项目typeof(AbpEntityFrameworkCoreSqlServerModule)为
typeof(AbpEntityFrameworkCoreMySQLModule)。
在这里插入图片描述
在这里插入图片描述
在解决方案的Acme.BookStore.EntityFrameworkCore.DbMigrations层找到BookStoreMigrationsDbContextFactory文件,并做如下修改:

修改前:
var builder = new DbContextOptionsBuilder<EyinzhangMigrationsDbContext>()
                .UseSqlServer(configuration.GetConnectionString("Default"));
修改后:
var builder = new DbContextOptionsBuilder<EyinzhangMigrationsDbContext>()
                .UseMySql(configuration.GetConnectionString("Default"));

![在这里插入图片描述](https://img-blog.csdnimg.cn/20200513172119498.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L1BhUGNfU3VlaQ==,size_16,color_FFFFFF,t_70

5、重新生成迁移文件并同步数据库

删除"Acme.BookStore.EntityFrameworkCore.DbMigrations”项目下的Migrator文件夹,将Acme.BookStore.Web项目设置为启动项目,然后执行add-migration生成迁移!

Add-Migration init

执行成功后 ,Migrations文件夹里会新增几个文件,如图:
在这里插入图片描述

然后执行update-database命令,同步数据库。

Update-Database

在执行update-database时,会提示如下错误:
在这里插入图片描述
以上问题,可使用下面的步骤解决:
在EntityFrameworkCore项目,EntityFrameworkCore文件夹下,添加IdentityServerModelCreatingExtensions类。代码如下:

    public static class IdentityServerModelCreatingExtensions
    {
        public static void ConfigureIdentityServerForMySQL(this ModelBuilder builder)
        {
            // Solve the problem of MySQL migration
            // https://github.com/abpframework/abp/issues/1920
    
            builder.Entity<ApiSecret>(b =>
            {
                // After trying, you can also set it to 400
                b.Property(x => x.Value).HasMaxLength(300);
            });
    
            builder.Entity<ClientPostLogoutRedirectUri>(b =>
            {
                b.Property(x => x.PostLogoutRedirectUri).HasMaxLength(300); // or 400 ?
            });
    
            builder.Entity<ClientRedirectUri>(b =>
            {
                b.Property(x => x.RedirectUri).HasMaxLength(300); // or 400 ?
            });
    
            builder.Entity<ClientSecret>(b =>
            {
                b.Property(x => x.Value).HasMaxLength(300); // or 400 ?
            });
    
        }
    }

找到BookStoreDbContext类中OnModelCreating方法,在方法末端添加以下代码:

  builder.ConfigureIdentityServer(options =>
            {
                options.DatabaseProvider = EfCoreDatabaseProvider.MySql;
            });
            builder.ConfigureIdentityServerForMySQL();

找到BookStoreDbContextModelCreatingExtensions类中ConfigureBookStore方法,在方法末端添加以下代码:

            builder.ConfigureIdentityServer(options =>
            {
                options.DatabaseProvider = EfCoreDatabaseProvider.MySql;
            });

全部操作完成后,再次删除"Acme.BookStore.EntityFrameworkCore.DbMigrations”项目下的Migrator文件夹,重新执行add-migration生成迁移,并执行update-database命令,同步数据库。成功!
参考:https://github.com/abpframework/abp/issues/2053

PS:如同步数据库遇到Specified key was too long; max key length is 767 bytes 问题,可参考下面的四种方法解决:
1、前缀索引,不使用列的全部内容作为索引,使用一部分,例如在原有的例子中我们使用了test列作为索引无法建表,但是如果改为使用test(255)作为索引就可以了,这就是所谓的使用前缀索引:
在这里插入图片描述
2、升级mysql到8.0以上版本;
3、缩小索引长度(256缩小到255以下);
4、修改字符集(utf-8修改到GBK);
参考:https://blog.csdn.net/hfut_wowo/article/details/88018800

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值