| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using ArturRios.Data.Relational.Core.Configuration; |
| | | 3 | | using ArturRios.Data.Relational.Core.Providers; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | |
| | | 6 | | namespace ArturRios.Data.MySql; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// <see cref="IDatabaseProvider"/> that configures EF Core to use MySQL or MariaDB via |
| | | 10 | | /// Microting.EntityFrameworkCore.MySql, a maintained fork of Pomelo.EntityFrameworkCore.MySql. |
| | | 11 | | /// The server version is detected from the server the first time a connection string is configured, |
| | | 12 | | /// then reused. |
| | | 13 | | /// </summary> |
| | | 14 | | public class MySqlProvider : IDatabaseProvider |
| | | 15 | | { |
| | | 16 | | // Context options are built once per DI scope (per request, in a web app), and detecting the |
| | | 17 | | // server version opens a connection and queries the server. The provider is a singleton, so the |
| | | 18 | | // detected version is kept per connection string: one probe per server instead of one per scope. |
| | | 19 | | // A failed probe is not cached, so the next scope retries it. |
| | 8 | 20 | | private readonly ConcurrentDictionary<string, ServerVersion> _serverVersions = new(StringComparer.Ordinal); |
| | | 21 | | |
| | | 22 | | /// <inheritdoc /> |
| | 4 | 23 | | public DatabaseType Type => DatabaseType.MySql; |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public void Configure(DbContextOptionsBuilder builder, string connectionString) => |
| | 24 | 27 | | builder.UseMySql(connectionString, _serverVersions.GetOrAdd(connectionString, ServerVersion.AutoDetect)); |
| | | 28 | | } |