-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Detect EF Core database provider by keyword match #25295
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/EfCoreDatabaseProviderHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| using System; | ||
|
|
||
| namespace Volo.Abp.EntityFrameworkCore; | ||
|
|
||
| public static class EfCoreDatabaseProviderHelper | ||
| { | ||
| public static EfCoreDatabaseProvider? GetDatabaseProviderOrNull(string? providerName) | ||
| { | ||
| if (providerName.IsNullOrWhiteSpace()) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| if (providerName.Contains("SqlServer", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return EfCoreDatabaseProvider.SqlServer; | ||
| } | ||
| if (providerName.Contains("Npgsql", StringComparison.OrdinalIgnoreCase) || | ||
| providerName.Contains("PostgreSQL", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return EfCoreDatabaseProvider.PostgreSql; | ||
| } | ||
| if (providerName.Contains("MySql", StringComparison.OrdinalIgnoreCase) || | ||
| providerName.Contains("Pomelo", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return EfCoreDatabaseProvider.MySql; | ||
| } | ||
| if (providerName.Contains("Oracle", StringComparison.OrdinalIgnoreCase) || | ||
| providerName.Contains("Devart", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return EfCoreDatabaseProvider.Oracle; | ||
| } | ||
| if (providerName.Contains("Sqlite", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return EfCoreDatabaseProvider.Sqlite; | ||
| } | ||
| if (providerName.Contains("InMemory", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return EfCoreDatabaseProvider.InMemory; | ||
| } | ||
| if (providerName.Contains("Firebird", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return EfCoreDatabaseProvider.Firebird; | ||
| } | ||
| if (providerName.Contains("Cosmos", StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return EfCoreDatabaseProvider.Cosmos; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...ityFrameworkCore.Tests/Volo/Abp/EntityFrameworkCore/EfCoreDatabaseProviderHelper_Tests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| using Microsoft.EntityFrameworkCore; | ||
| using Shouldly; | ||
| using Xunit; | ||
|
|
||
| namespace Volo.Abp.EntityFrameworkCore; | ||
|
|
||
| public class EfCoreDatabaseProviderHelper_Tests | ||
| { | ||
| [Fact] | ||
| public void Should_Detect_SqlServer_From_Real_Assembly() | ||
| { | ||
| var builder = new DbContextOptionsBuilder<EmptyDbContext>(); | ||
| Microsoft.EntityFrameworkCore.SqlServerDbContextOptionsExtensions.UseSqlServer(builder, "Server=localhost;Database=test"); | ||
| using var context = new EmptyDbContext(builder.Options); | ||
| EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(context.Database.ProviderName) | ||
| .ShouldBe(EfCoreDatabaseProvider.SqlServer); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_Detect_PostgreSql_From_Real_Assembly() | ||
| { | ||
| var builder = new DbContextOptionsBuilder<EmptyDbContext>(); | ||
| Microsoft.EntityFrameworkCore.NpgsqlDbContextOptionsBuilderExtensions.UseNpgsql(builder, "Host=localhost;Database=test"); | ||
| using var context = new EmptyDbContext(builder.Options); | ||
| EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(context.Database.ProviderName) | ||
| .ShouldBe(EfCoreDatabaseProvider.PostgreSql); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_Detect_MySql_Pomelo_From_Assembly_Name() | ||
| { | ||
| var providerName = typeof(Microsoft.EntityFrameworkCore.MySqlDbContextOptionsBuilderExtensions).Assembly.GetName().Name; | ||
| EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(providerName) | ||
| .ShouldBe(EfCoreDatabaseProvider.MySql); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_Detect_MySql_Oracle_From_Real_Assembly() | ||
| { | ||
| var builder = new DbContextOptionsBuilder<EmptyDbContext>(); | ||
| Microsoft.EntityFrameworkCore.MySQLDbContextOptionsExtensions.UseMySQL(builder, "Server=localhost;Database=test"); | ||
| using var context = new EmptyDbContext(builder.Options); | ||
| EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(context.Database.ProviderName) | ||
| .ShouldBe(EfCoreDatabaseProvider.MySql); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_Detect_Oracle_From_Real_Assembly() | ||
| { | ||
| var builder = new DbContextOptionsBuilder<EmptyDbContext>(); | ||
| Microsoft.EntityFrameworkCore.OracleDbContextOptionsExtensions.UseOracle(builder, "Data Source=localhost/XE"); | ||
| using var context = new EmptyDbContext(builder.Options); | ||
| EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(context.Database.ProviderName) | ||
| .ShouldBe(EfCoreDatabaseProvider.Oracle); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_Detect_Sqlite_From_Real_Assembly() | ||
| { | ||
| var builder = new DbContextOptionsBuilder<EmptyDbContext>(); | ||
| Microsoft.EntityFrameworkCore.SqliteDbContextOptionsBuilderExtensions.UseSqlite(builder, "Data Source=:memory:"); | ||
| using var context = new EmptyDbContext(builder.Options); | ||
| EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(context.Database.ProviderName) | ||
| .ShouldBe(EfCoreDatabaseProvider.Sqlite); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void Should_Detect_InMemory_From_Real_Assembly() | ||
| { | ||
| var builder = new DbContextOptionsBuilder<EmptyDbContext>(); | ||
| Microsoft.EntityFrameworkCore.InMemoryDbContextOptionsExtensions.UseInMemoryDatabase(builder, "test"); | ||
| using var context = new EmptyDbContext(builder.Options); | ||
| EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(context.Database.ProviderName) | ||
| .ShouldBe(EfCoreDatabaseProvider.InMemory); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("Devart.Data.Oracle.Entity.EFCore", EfCoreDatabaseProvider.Oracle)] | ||
| [InlineData("FirebirdSql.EntityFrameworkCore.Firebird", EfCoreDatabaseProvider.Firebird)] | ||
| [InlineData("Microsoft.EntityFrameworkCore.Cosmos", EfCoreDatabaseProvider.Cosmos)] | ||
| public void Should_Detect_Providers_Without_Package_Reference(string providerName, EfCoreDatabaseProvider expected) | ||
| { | ||
| EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(providerName).ShouldBe(expected); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("microsoft.entityframeworkcore.sqlserver", EfCoreDatabaseProvider.SqlServer)] | ||
| [InlineData("POMELO.ENTITYFRAMEWORKCORE.MYSQL", EfCoreDatabaseProvider.MySql)] | ||
| [InlineData("npgsql.entityframeworkcore.postgresql", EfCoreDatabaseProvider.PostgreSql)] | ||
| public void Should_Detect_Providers_Case_Insensitively(string providerName, EfCoreDatabaseProvider expected) | ||
| { | ||
| EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(providerName).ShouldBe(expected); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData(null)] | ||
| [InlineData("")] | ||
| [InlineData(" ")] | ||
| [InlineData("Some.Unknown.Provider")] | ||
| public void Should_Return_Null_For_Unknown_Or_Empty(string? providerName) | ||
| { | ||
| EfCoreDatabaseProviderHelper.GetDatabaseProviderOrNull(providerName).ShouldBeNull(); | ||
| } | ||
|
|
||
| private class EmptyDbContext : DbContext | ||
| { | ||
| public EmptyDbContext(DbContextOptions<EmptyDbContext> options) : base(options) | ||
| { | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.