From 6d7b49df4d8b7fcd3b5a804230278f3183861b06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Thu, 28 May 2020 23:58:48 +0300 Subject: [PATCH] Try to automatically set the database provider. --- .../Abp/EntityFrameworkCore/AbpDbContext.cs | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs index 2b35523916..1f1cf4953e 100644 --- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs +++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/AbpDbContext.cs @@ -92,6 +92,8 @@ namespace Volo.Abp.EntityFrameworkCore { base.OnModelCreating(modelBuilder); + TrySetDatabaseProvider(modelBuilder); + foreach (var entityType in modelBuilder.Model.GetEntityTypes()) { ConfigureBasePropertiesMethodInfo @@ -108,6 +110,40 @@ namespace Volo.Abp.EntityFrameworkCore } } + protected virtual void TrySetDatabaseProvider(ModelBuilder modelBuilder) + { + var provider = GetDatabaseProviderOrNull(modelBuilder); + if (provider != null) + { + modelBuilder.SetDatabaseProvider(provider.Value); + } + } + + protected virtual EfCoreDatabaseProvider? GetDatabaseProviderOrNull(ModelBuilder modelBuilder) + { + switch (Database.ProviderName) + { + case "Microsoft.EntityFrameworkCore.SqlServer": + return EfCoreDatabaseProvider.SqlServer; + case "Npgsql.EntityFrameworkCore.PostgreSQL": + return EfCoreDatabaseProvider.PostgreSql; + case "Pomelo.EntityFrameworkCore.MySql": + return EfCoreDatabaseProvider.MySql; + case "Devart.Data.Oracle.EFCore": + return EfCoreDatabaseProvider.Oracle; + case "Microsoft.EntityFrameworkCore.Sqlite": + return EfCoreDatabaseProvider.Sqlite; + case "Microsoft.EntityFrameworkCore.InMemory": + return EfCoreDatabaseProvider.InMemory; + case "FirebirdSql.EntityFrameworkCore.Firebird": + return EfCoreDatabaseProvider.Firebird; + case "Microsoft.EntityFrameworkCore.Cosmos": + return EfCoreDatabaseProvider.Cosmos; + default: + return null; + } + } + public override async Task SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default) { try