diff --git a/src/EFCore.MySql/Scaffolding/Internal/MySqlCodeGenerator.cs b/src/EFCore.MySql/Scaffolding/Internal/MySqlCodeGenerator.cs index ef635f5c7..03a4bd58b 100644 --- a/src/EFCore.MySql/Scaffolding/Internal/MySqlCodeGenerator.cs +++ b/src/EFCore.MySql/Scaffolding/Internal/MySqlCodeGenerator.cs @@ -4,7 +4,9 @@ using JetBrains.Annotations; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; +using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Scaffolding; +using Pomelo.EntityFrameworkCore.MySql.Infrastructure.Internal; using Pomelo.EntityFrameworkCore.MySql.Storage.Internal; namespace Pomelo.EntityFrameworkCore.MySql.Scaffolding.Internal @@ -15,13 +17,14 @@ namespace Pomelo.EntityFrameworkCore.MySql.Scaffolding.Internal /// public class MySqlCodeGenerator : ProviderCodeGenerator { - /// - /// Initializes a new instance of the class. - /// - /// The dependencies. - public MySqlCodeGenerator([NotNull] ProviderCodeGeneratorDependencies dependencies) + private readonly IMySqlOptions _options; + + public MySqlCodeGenerator( + [NotNull] ProviderCodeGeneratorDependencies dependencies, + IMySqlOptions options) : base(dependencies) { + _options = options; } /// @@ -41,6 +44,18 @@ public override MethodCallCodeFragment GenerateUseProvider( ? new object[] {connectionString} : new object[] {connectionString, new NestedClosureCodeFragment("x", providerOptions)}); } + + public override MethodCallCodeFragment GenerateProviderOptions() + { + var serverVersionCall = new MethodCallCodeFragment( + nameof(MySqlDbContextOptionsBuilder.ServerVersion), + _options.ServerVersion.ToString()); + + var providerOptions = base.GenerateProviderOptions(); + + return providerOptions == null + ? serverVersionCall + : serverVersionCall.Chain(providerOptions); + } } } - diff --git a/src/EFCore.MySql/Scaffolding/Internal/MySqlDatabaseModelFactory.cs b/src/EFCore.MySql/Scaffolding/Internal/MySqlDatabaseModelFactory.cs index 5f2e9b04d..4736d219d 100644 --- a/src/EFCore.MySql/Scaffolding/Internal/MySqlDatabaseModelFactory.cs +++ b/src/EFCore.MySql/Scaffolding/Internal/MySqlDatabaseModelFactory.cs @@ -20,6 +20,7 @@ using Pomelo.EntityFrameworkCore.MySql.Infrastructure.Internal; using Pomelo.EntityFrameworkCore.MySql.Internal; using Pomelo.EntityFrameworkCore.MySql.Metadata.Internal; +using Pomelo.EntityFrameworkCore.MySql.Storage; using Pomelo.EntityFrameworkCore.MySql.Storage.Internal; namespace Pomelo.EntityFrameworkCore.MySql.Scaffolding.Internal @@ -97,7 +98,27 @@ public override DatabaseModel Create(DbConnection connection, DatabaseModelFacto private void SetupMySqlOptions(DbConnection connection) { var optionsBuilder = new DbContextOptionsBuilder(); - optionsBuilder.UseMySql(connection); + optionsBuilder.UseMySql(connection, builder => + { + // Set the actual server version from the open connection here, so we can + // access it from IMySqlOptions later when generating the code for the + // `UseMySql()` call. + if (_options.ServerVersion.IsDefault) + { + try + { + var mySqlConnection = (MySqlConnection)connection; + builder.ServerVersion(new ServerVersion(mySqlConnection.ServerVersion)); + } + catch (InvalidOperationException) + { + // If we cannot determine the server version for some reason, just fall + // back on the latest one (the default). + + // TODO: Output warning. + } + } + }); if (Equals(_options, new MySqlOptions())) {