ABP更换MySql数据库
原因:ABP默认使用的数据库是sqlServer,本地没有安装sqlServer,安装的是mysql,需要更换数据库
ABP版本:9.0
此处以官网TodoApp
项目为例
打开EntityFrameworkCore程序集,可以看到默认使用的是sqlServer,此处截图为已安装mysql依赖包
步骤一、安装mysql依赖包
如果没安装 ABP CLI ,先安装
dotnet tool install -g Volo.Abp.Studio.Cli
安装ABP依赖包
abp add-package Volo.Abp.EntityFrameworkCore.MySQL
安装完mysql依赖包后,sqlserve依赖包可以删除也可以留着
步骤二、更改TodoAppEntityFrameworkCoreModule.cs文件
修改完成代码如下
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.Web
和 TodoApp.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
中连接字符串错误,
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!