Switch EF Core Connection from SQL Server to a Different Database Provider
- 4 minutes to read
The XAF Wizard creates applications that use the Microsoft SQL Server. To use a different database provider, modify your XAF solution. Use the EFCoreProvider
parameter to specify the provider type.
#PostgreSQL
Install the Npgsql.EntityFrameworkCore.PostgreSQL package. Note that the version of the Microsoft.EntityFrameworkCore package reference must be compatible with the Entity Framework version that your solution uses.
Change your application’s connection string.
"ConnectionStrings": { "ConnectionString": "EFCoreProvider=PostgreSql;Host=localhost;Database=my_db;Username=postgres;Password=qwerty",
If you use migrations, change the
MySolutionDesignTimeDbContextFactory.ConnectionString
property as follows:public class MySolutionDesignTimeDbContextFactory : DesignTimeDbContextFactory<MySolutionEFCoreDbContext> { protected override string ConnectionString => "EFCoreProvider=PostgreSql;Host=localhost;Database=my_db2;Username=postgres;Password=qwerty"; }
#MySQL
Install the Pomelo.EntityFrameworkCore.MySql package. Note that the version of the Microsoft.EntityFrameworkCore package reference must be compatible with the Entity Framework version that your solution uses.
Change your application’s connection string.
"ConnectionStrings": { "ConnectionString": "EFCoreProvider=MySQL; server=localhost; database=MySQLSolution; user=root; password=qwerty",
If you use migrations, change the
MySolutionDesignTimeDbContextFactory.ConnectionString
property as follows:public class MySQLSolutionDesignTimeDbContextFactory : DesignTimeDbContextFactory<MySQLSolutionEFCoreDbContext> { protected override string ConnectionString => "EFCoreProvider=MySQL; server=localhost; database=MySQLSolution; user=root; password=qwerty" }
#Supported Database Providers
This section contains a list of EF Core providers supported by XAF applications. Use the EFCoreProvider
parameter in the connection string to specify the database provider type used in your application.
Database | Provider Nu |
EFCore |
---|---|---|
Microsoft SQL Server | Microsoft. |
Sql |
Microsoft SQL Azure | Microsoft. |
Sql |
My |
Pomelo. |
My |
Postgre |
Npgsql. |
Postgre |
Oracle | Oracle. |
Oracle |
Firebird | Firebird |
Firebird |
SQLite | Microsoft. |
SQLite |
In |
Microsoft. |
In |
To configure an application with a non-listed database provider, follow these steps:
- Install the relevant EFCore provider for your database. Refer to the following topic for a list of available EF Core providers: Database Providers.
Change your application’s connection string. Do not specify the
EFCoreProvider
parameter."ConnectionStrings": { "ConnectionString": "<write your connection string here>",
If you use migrations, change the
MySolutionDesignTimeDbContextFactory.CreateDbContext
method to use your database provider and the updated connection string. Call the appropriate extension method to set up the provider and connection string:options.UseYourConnectionProvider(connectionString)
. This method and its parameters can be found in the official documentation for the EFCore provider being used.public class MySolutionDesignTimeDbContextFactory : DesignTimeDbContextFactory<MySolutionDbContext> { public MySolutionDbContext CreateDbContext(string[] args) { var optionsBuilder = new DbContextOptionsBuilder<MySolutionDbContext>(); optionsBuilder.UseYourConnectionProvider(connectionString); optionsBuilder.UseChangeTrackingProxies(); optionsBuilder.UseObjectSpaceLinkProxies(); return new MySolutionDbContext(optionsBuilder.Options); }
Change the following files to use your database provider:
public class Startup { public void ConfigureServices(IServiceCollection services) { //... builder.ObjectSpaceProviders .AddSecuredEFCore().WithDbContext<MySolution.Module.BusinessObjects.MySolutionDbContext>((serviceProvider, options) => { options.UseYourConnectionProvider(connectionString); options.UseChangeTrackingProxies(); options.UseObjectSpaceLinkProxies(); })
#SQLite and Other File-Based Databases
We do not recommend using SQLite with XAF in production scenarios with large amounts of data. SQLite is a file-based database that, due to its design, is inferior in performance and other features to powerful distributed server-based RDBMSs such as SQL Server, Oracle, MySQL, or PostgreSQL (used by the majority of our customers). If you experience performance issues with SQLite and other file-based databases (even with our Server Mode data sources), we cannot offer any suitable solutions other than switching to another RDBMS.
We can only recommend that our customers use SQLite for demo/testing purposes or simple applications that do not have many records. NOTE: as a developer or tester, you can also use the databases that can be installed with Visual Studio: Microsoft SQL Server Express or LocalDB databases (a free edition of SQL Server, ideal for development and production for desktop, web, and small server applications).
For XPO-based applications, refer to the following topic to learn how to connect these applications to different database providers: Database Systems Supported by XPO.