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:
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 Volo.Abp.EntityFrameworkCore.SqlServer NuGet package dependency from the
- Add the Pomelo.EntityFrameworkCore.MySql NuGet package dependency to your
Remove the Module Dependency
AbpEntityFrameworkCoreSqlServerModule from the dependency list of your YourProjectNameEntityFrameworkCoreModule class.
Change the UseSqlServer() Calls
Find the following code part inside the YourProjectNameEntityFrameworkCoreModule class:
Replace it with the following code part:
if (ctx.ExistingConnection != null)
UseMySqlcalls 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.ConnectionStringand passes to the
UseMySqlif there is no active connection (which will cause to create a new database connection). Using the
ctx.ConnectionStringis 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
.Web projects, but it depends on your solution structure.
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
Auth Server module.
options.DatabaseProvider = EfCoreDatabaseProvider.MySql;
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
.EntityFrameworkCoreproject and re-build the solution.
Add-Migration "Initial"on the Package Manager Console (select the
.Web) project as the startup project in the Solution Explorer and select the
.EntityFrameworkCoreproject as the default project in the Package Manager Console).
This will create a database migration with all database objects (tables) configured.
.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
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.