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

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

ABP版本:9.0

此处以官网TodoApp项目为例

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

步骤一、安装mysql依赖包

https://abp.io/packages

如果没安装 ABP CLI ,先安装

shell 复制代码
dotnet tool install -g Volo.Abp.Studio.Cli

安装ABP依赖包

shell 复制代码
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>

修改完成代码如下

csharp 复制代码
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

csharp 复制代码
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程序集下

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

步骤五、数据库迁移

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

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

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

执行迁移命令

shell 复制代码
add-migration "initial"

更新数据库命令

shell 复制代码
update-database

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

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

相关推荐
枯萎穿心攻击5 小时前
ECS由浅入深第三节:进阶?System 的行为与复杂交互模式
开发语言·unity·c#·游戏引擎
小码编匠6 小时前
WPF 自定义TextBox带水印控件,可设置圆角
后端·c#·.net
水果里面有苹果6 小时前
17-C#的socket通信TCP-1
开发语言·tcp/ip·c#
阿蒙Amon13 小时前
C# Linq to SQL:数据库编程的解决方案
数据库·c#·linq
iCxhust16 小时前
c# U盘映像生成工具
开发语言·单片机·c#
emplace_back17 小时前
C# 集合表达式和展开运算符 (..) 详解
开发语言·windows·c#
阿蒙Amon18 小时前
为什么 12 版仍封神?《C# 高级编程》:从.NET 5 到实战架构,进阶者绕不开的必修课
开发语言·c#
深海潜水员19 小时前
【Behavior Tree】-- 行为树AI逻辑实现- Unity 游戏引擎实现
游戏·unity·c#
开开心心_Every19 小时前
便捷的Office批量转PDF工具
开发语言·人工智能·r语言·pdf·c#·音视频·symfony