I am in the process of migrating a large solution from .NET Framework 4.8 to .NET 9.0. I used .NET Upgrade Assistant to get started. I have a long way to go.
All of the projects in the solution have their own static DataAccess class using System.Data.SqlClient for all database interaction. (That's a problem I'll deal with next.)
How do I get the connection string from appsettings.json into a static class, where I can't call a constructor?
namespace MyProject
{
public static class DataAccess
{
/* LEGACY:
private static string m_ConnectionString =
ConfigurationManager.ConnectionStrings["DefaultConnStrg"].ConnectionString;
*/
private static string m_ConnectionString = string.Empty;
private static IConfiguration m_Config;
// This will not compile! "CS0710: Static classes cannot have instance constructors."
public ShopDataAccess( IConfiguration configuration )
{
m_Config = configuration;
m_ConnectionString = m_Config.GetConnectionString( "DefaultConnStrg" );
}
...
}
