Entity Framework on .NET FX (full framework) has always worked with ConfigurationManager until EF Core 3.1 (maybe 3.0). The commands listed below previously always just worked by detecting your connection string values in your "app.config" and "web.config" configuration files. However with the upgrade from EF Core 2.2 to EF Core 3.1 it stopped working... why?

Add-Migration Initial -Project Speedy.Website.Data.Sql -StartupProject Speedy.Website
Add-Migration Initial -Project Speedy.Website.Data.Sqlite -StartupProject Speedy.Benchmark

The following code that used to work will now fail...

var connection = ConfigurationManager.ConnectionStrings["DefaultConnection"];
if (connection != null && !string.IsNullOrWhiteSpace(connection.ConnectionString))
{
	return connection.ConnectionString;
}

However here is what you can do to make it work.

public static string GetConnectionString()
{
	var connection = ConfigurationManager.ConnectionStrings["DefaultConnection"];
	if (connection != null && !string.IsNullOrWhiteSpace(connection.ConnectionString))
	{
		return connection.ConnectionString;
	}

	if (connection == null)
	{
		// NOTE: EF 3.1 design tools no longer initializes ConfigurationManager  
		// so we have to load it manually for Add-Migration, grrrr
		// https://github.com/dotnet/efcore/issues/19760
		connection = GetFromAppConfig();

		if (connection != null)
		{
			return connection.ConnectionString;
		}
	}

	var configuration = new ConfigurationBuilder()
		.SetBasePath(Directory.GetCurrentDirectory())
		.AddJsonFile("AppSettings.json", true)
		.Build();

	return configuration.GetConnectionString("DefaultConnection");
}

private static ConnectionStringSettings GetFromAppConfig()
{
	var directory = Environment.CurrentDirectory;
	var configFileNames = new[] { "web.config", "app.config" };

	foreach (var configFileName in configFileNames)
	{
		var filePath = Path.Combine(directory, configFileName);
		
		if (File.Exists(filePath))
		{
			var map = new ExeConfigurationFileMap { ExeConfigFilename = filePath };
			var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
			var connection = config.ConnectionStrings.ConnectionStrings["DefaultConnection"];
			if (connection != null)
			{
				return connection;
			}
		}
	}

	return null;
}

Here is a link to the bug report with the team response. The initial team response was meh. I hope they come back with some more details but only time will tell.

https://github.com/dotnet/efcore/issues/19760

I don't know what the EF team changed that changed the Add-Migration that no longer initializes ConfigurationManager. Now we have to manually load our configuration. I hope this was in error but my assumption is it was in the name of "progression". I also think it's lack of testing or team usage of EF in .NET FX.

We have to progress our industry but sometimes progression is not actually progress. I switch to EF Core because it was my understanding that EF core was going to continue to support .NET FX (full framework). However with EF Core 3.0 the team move to .NET standard 2.1 which left the .NET FX developers with no way to upgrade. However the team decided (imo correctly) to roll back to .NET standard 2.0 with EF Core 3.1. Now I feel the lack of support for ConfigurationManager is very similar issue.

I feel the team should continue to support .NET FX until there is a real upgrade path. I feel the .NET platform is split between .NET FX and .NET Core. I'm really hopeful .NET 5 will bring the .NET platform back together.