Switch to another DBMS for Entity Framework Core
The application startup template comes with SQL Server provider pre-configured for the Entity Framework Core. Entity Framework Core supports many other DBMSs and you can use any of them with your ABP based applications.
ABP framework provides integration packages for some common DBMSs to make the configuration a bit easier (see the entity framework core document for a list of available integration packages). However, you can 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 manually if there is no integration package for your DBMS provider.
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
.EntityFrameworkCore
project. - Add the Pomelo.EntityFrameworkCore.MySql NuGet package dependency to your
.EntityFrameworkCore
project.
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 theUseMySql
if there is no active connection (which will cause to create a new database connection). Using thectx.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.
Change the Migrations DbContext
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 startup template contains a YourProjectNameMigrationsDbContext which is responsible to maintain and migrate the database schema. This DbContext basically calls extension methods of the depended modules to configure their database tables.
Open the YourProjectNameMigrationsDbContext and change the builder.ConfigureIdentityServer();
line as shown below:
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.DbMigrations
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.DbMigrations
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.