diff --git a/src/ef/Commands/ProjectCommandBase.cs b/src/ef/Commands/ProjectCommandBase.cs index 4d026c4ccbd..81901d94251 100644 --- a/src/ef/Commands/ProjectCommandBase.cs +++ b/src/ef/Commands/ProjectCommandBase.cs @@ -7,6 +7,10 @@ using Microsoft.DotNet.Cli.CommandLine; using Microsoft.EntityFrameworkCore.Tools.Properties; +#if NET461 +using System.Configuration; +#endif + namespace Microsoft.EntityFrameworkCore.Tools.Commands { internal abstract class ProjectCommandBase : EFCommandBase @@ -60,6 +64,27 @@ protected IOperationExecutor CreateExecutor() } catch (MissingMethodException) // NB: Thrown with EF Core 3.1 { + var configurationFile = (_startupAssembly.Value() ?? _assembly.Value()) + ".config"; + if (File.Exists(configurationFile)) + { + AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", configurationFile); + try + { + typeof(ConfigurationManager) + .GetField("s_initState", BindingFlags.Static | BindingFlags.NonPublic) + .SetValue(null, 0); + typeof(ConfigurationManager) + .GetField("s_configSystem", BindingFlags.Static | BindingFlags.NonPublic) + .SetValue(null, null); + typeof(ConfigurationManager).Assembly + .GetType("System.Configuration.ClientConfigPaths") + .GetField("s_current", BindingFlags.Static | BindingFlags.NonPublic) + .SetValue(null, null); + } + catch + { + } + } } #elif !NETCOREAPP2_0 #error target frameworks need to be updated. diff --git a/src/ef/ef.csproj b/src/ef/ef.csproj index 9e95985c3b6..26ebe4e7438 100644 --- a/src/ef/ef.csproj +++ b/src/ef/ef.csproj @@ -25,6 +25,7 @@ +