当前位置: 首页 > news >正文

【无标题】ABP更换MySql数据库

原因:ABP默认使用的数据库是sqlServer,本地没有安装sqlServer,安装的是mysql,需要更换数据库

ABP版本:9.0

此处以官网TodoApp项目为例

打开EntityFrameworkCore程序集,可以看到默认使用的是sqlServer,此处截图为已安装mysql依赖包

步骤一、安装mysql依赖包

https://abp.io/packages

如果没安装 ABP CLI ,先安装

dotnet tool install -g Volo.Abp.Studio.Cli

安装ABP依赖包

abp add-package Volo.Abp.EntityFrameworkCore.MySQL

安装完mysql依赖包后,sqlserve依赖包可以删除也可以留着

步骤二、更改TodoAppEntityFrameworkCoreModule.cs文件

  • 1、将 <font style="color:rgb(214, 51, 132);">UseSqlServer()</font> 改为 <font style="color:rgb(214, 51, 132);">UseMySQL()</font>
  • 2、引入 Volo.Abp.EntityFrameworkCore.MySQL 命名空间
  • 3、将 <font style="color:rgb(214, 51, 132);">typeof(AbpEntityFrameworkCoreSqlServerModule</font>) 替换为 <font style="color:rgb(214, 51, 132);">typeof(AbpEntityFrameworkCoreMySQLModule)</font>

修改完成代码如下

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AuditLogging.EntityFrameworkCore;
using Volo.Abp.BackgroundJobs.EntityFrameworkCore;
using Volo.Abp.BlobStoring.Database.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.MySQL;
using Volo.Abp.FeatureManagement.EntityFrameworkCore;
using Volo.Abp.Identity.EntityFrameworkCore;
using Volo.Abp.Modularity;
using Volo.Abp.OpenIddict.EntityFrameworkCore;
using Volo.Abp.PermissionManagement.EntityFrameworkCore;
using Volo.Abp.SettingManagement.EntityFrameworkCore;
using Volo.Abp.Studio;
using Volo.Abp.TenantManagement.EntityFrameworkCore;namespace TodoApp.EntityFrameworkCore;[DependsOn(typeof(TodoAppDomainModule),typeof(AbpPermissionManagementEntityFrameworkCoreModule),typeof(AbpSettingManagementEntityFrameworkCoreModule),typeof(AbpEntityFrameworkCoreMySQLModule),typeof(AbpBackgroundJobsEntityFrameworkCoreModule),typeof(AbpAuditLoggingEntityFrameworkCoreModule),typeof(AbpFeatureManagementEntityFrameworkCoreModule),typeof(AbpIdentityEntityFrameworkCoreModule),typeof(AbpOpenIddictEntityFrameworkCoreModule),typeof(AbpTenantManagementEntityFrameworkCoreModule),typeof(BlobStoringDatabaseEntityFrameworkCoreModule))]
public class TodoAppEntityFrameworkCoreModule : AbpModule
{public override void PreConfigureServices(ServiceConfigurationContext context){TodoAppEfCoreEntityExtensionMappings.Configure();}public override void ConfigureServices(ServiceConfigurationContext context){context.Services.AddAbpDbContext<TodoAppDbContext>(options =>{/* Remove "includeAllEntities: true" to create* default repositories only for aggregate roots */options.AddDefaultRepositories(includeAllEntities: true);});if (AbpStudioAnalyzeHelper.IsInAnalyzeMode){return;}Configure<AbpDbContextOptions>(options =>{/* The main point to change your DBMS.* See also TodoAppDbContextFactory for EF Core tooling. */options.UseMySQL();});}
}

步骤三、修改TodoAppStoreDbContextFactory.cs

UseSqlServer 修改为 UseMySQL

public TodoAppDbContext CreateDbContext(string[] args)
{var configuration = BuildConfiguration();TodoAppEfCoreEntityExtensionMappings.Configure();var builder = new DbContextOptionsBuilder<TodoAppDbContext>().UseMySql(configuration.GetConnectionString("Default"), ServerVersion.Parse("5.7.38-mysql"));return new TodoAppDbContext(builder.Options);
}

步骤四、更改连接字符串

更改appsettings.json配置文件下的连接字符串,TodoApp.WebTodoApp.DbMigrator程序集下

"ConnectionStrings": {"Default": "Server=120.79.25.229;database=todoDatabase;uid=root;pwd=1126438236@qq.com;"},

步骤五、数据库迁移

1、删除TodoApp.EntityFrameworkCore程序集下Migrations文件夹中的所有文件并重新构建解决方案

如果没删除,迁移的时候会报错

2、在包管理控制台上执行迁移命令,注意,默认项目要选 TodoApp.EntityFrameworkCore

执行迁移命令

add-migration "initial"

更新数据库命令

update-database

数据库迁移过程中遇到的错误

1、配置文件appsettings.json中连接字符串错误,

http://www.lryc.cn/news/545215.html

相关文章:

  • 大模型微调入门(Transformers + Pytorch)
  • 【开源免费】基于SpringBoot+Vue.JS网络海鲜市场系统(JAVA毕业设计)
  • 在线会议时, 笔记本电脑的麦克风收音效果差是为什么
  • 理解文件系统
  • 第二十四:5.2【搭建 pinia 环境】axios 异步调用数据
  • Vue2+Element实现Excel文件上传下载预览【超详细图解】
  • C# 装箱(Boxing)与拆箱(Unboxing)
  • 【AD】3-10 原理图PDF导出
  • SQL命令详解之增删改数据
  • Docker 部署 MinIO 对象存储服务
  • IP段转CIDR:原理Java实现
  • 翻译: 深入分析LLMs like ChatGPT 一
  • springboot之HTML与图片生成
  • 数据结构(初阶)(三)----单链表
  • ChatGPT与DeepSeek:AI语言模型的巅峰对决
  • DaoCloud 亮相 2025 GDC丨开源赋能 AI 更多可能
  • 人工智能之数学基础:线性代数中矩阵的运算
  • (上)基于机器学习的图像识别——遥感图像分类(LeNet-5;AlexNet;VGGNet;GoogLeNet;ResNet)
  • 数据集笔记:NUSMods API
  • HTML元素,标签到底指的哪块部分?单双标签何时使用?
  • 基于ai技术的视频生成工具
  • 【Java 后端】Restful API 接口
  • Matlab地图绘制教程第2期—水陆填充图
  • 企业知识库搭建:14款开源与免费系统选择
  • 【Linux系统】—— 冯诺依曼体系结构与操作系统初理解
  • Android内存优化指南:从数据结构到5R法则的全面策略
  • 机器学习:线性回归,梯度下降,多元线性回归
  • Linux上用C++和GCC开发程序实现两个不同MySQL实例下单个Schema稳定高效的数据迁移到其它MySQL实例
  • RabbitMQ系列(一)架构解析
  • XSL 语言:XML 样式表的语言基础与应用