Switch to Another DBMS for Entity Framework Core

ABP CLI provides a -dbms option to allow you to choose your Database Management System (DBMS) while creating a new solution. It accepts the following values:

  • SqlServer (default)
  • MySQL
  • SQLite
  • Oracle
  • Oracle-Devart
  • PostgreSQL

So, if you want to use MySQL for your solution, you can use the -dbms MySQL option while using the abp new command. Example:

abp new BookStore -dbms MySQL

Also, the Get Started page on the ABP website allows you to select one of the providers.

This document provides guidance for who wants to manually change their DBMS after creating the solution.

You can use the following documents to learn how to switch to your favorite DBMS:

You can also configure your DBMS provider without these integration packages. While using the integration package is always recommended (it also makes standard for the depended version across different modules), you can do it yourself if there is no integration package for your DBMS provider.

For an example, this document explains how to switch to MySQL without using the MySQL integration package.

Replace the SQL Server Dependency

Remove the Module Dependency

Remove the AbpEntityFrameworkCoreSqlServerModule from the dependency list of your YourProjectNameEntityFrameworkCoreModule class.

Change the UseSqlServer() Calls

Find the following code part inside the YourProjectNameEntityFrameworkCoreModule class:

Configure<AbpDbContextOptions>(options =>
{
    options.UseSqlServer();
});

Replace it with the following code part:

Configure<AbpDbContextOptions>(options =>
{
    options.Configure(ctx =>
    {
        if (ctx.ExistingConnection != null)
        {
            ctx.DbContextOptions.UseMySql(ctx.ExistingConnection);
        }
        else
        {
            ctx.DbContextOptions.UseMySql(ctx.ConnectionString);
        }
    });
});
  • UseMySql calls in this code is defined by the Pomelo.EntityFrameworkCore.MySql package and you can use its additional options if you need.
  • This code first checks if there is an existing (active) connection to the same database in the current request and reuses it if possible. This allows to share a single transaction among different DbContext types. ABP handles the rest of the things.
  • It uses ctx.ConnectionString and passes to the UseMySql if there is no active connection (which will cause to create a new database connection). Using the ctx.ConnectionString is important here. Don't pass a static connection string (or a connection string from a configuration). Because ABP dynamically determines the correct connection string in a multi-database or multi-tenant environment.

Change the Connection Strings

MySQL connection strings are different than SQL Server connection strings. So, check all appsettings.json files in your solution and replace the connection strings inside them. See the connectionstrings.com for details of MySQL connection string options.

You typically will change the appsettings.json inside the .DbMigrator and .Web projects, but it depends on your solution structure.

DBMS restrictions

MySQL DBMS has some slight differences than the SQL Server. Some module database mapping configuration (especially the field lengths) causes problems with MySQL. For example, some of the the IdentityServer module tables has such problems and it provides an option to configure the fields based on your DBMS.

The module may provide some built-in solutions. You can configure it via ModelBuilder. eg: Auth Server module.

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

Then ConfigureIdentityServer() method will set the field lengths to not exceed the MySQL limits. Refer to related module documentation if you have any problem while creating or executing the database migrations.

Re-Generate the Migrations

The startup template uses Entity Framework Core's Code First Migrations. EF Core Migrations depend on the selected DBMS provider. So, changing the DBMS provider will cause the migration fails.

  • Delete the Migrations folder under the .EntityFrameworkCore project and re-build the solution.
  • Run Add-Migration "Initial" on the Package Manager Console (select the .DbMigrator (or .Web) project as the startup project in the Solution Explorer and select the .EntityFrameworkCore project as the default project in the Package Manager Console).

This will create a database migration with all database objects (tables) configured.

Run the .DbMigrator project to create the database and seed the initial data.

Run the Application

It is ready. Just run the application and enjoy coding.

Related discussions: https://github.com/abpframework/abp/issues/1920

Contributors


Last updated: July 31, 2024 Edit this page on GitHub

Was this page helpful?

Please make a selection.

To help us improve, please share your reason for the negative feedback in the field below.

Please enter a note.

Thank you for your valuable feedback!

Please note that although we cannot respond to feedback, our team will use your comments to improve the experience.

In this document
Community Talks

What’s New with .NET 9 & ABP 9?

21 Nov, 17:00
Online
Register Now
Mastering ABP Framework Book
Mastering ABP Framework

This book will help you gain a complete understanding of the framework and modern web application development techniques.

Learn More