最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

asp.net core - Migrating from .NET Framework to .NET 9.0, How to access ConnectionStrings from a static class? (web.config vs ap

programmeradmin2浏览0评论

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" );
        }
    ...
}

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" );
        }
    ...
}
Share Improve this question edited Mar 19 at 5:53 Zhi Lv 22k1 gold badge27 silver badges37 bronze badges asked Mar 13 at 21:46 TrintTrint 294 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

My guess is that a lot of problems will be resolved once you move away from the static DataAccess class. It seems that your legacy app spun its own Factory-like scheme that can be handled cleanly using native .NET & EntityFrameworkCore.

DbContext instances (or DbContextFactory instances) can be set up to be available from the IServiceProvider, or injected directly into controllers or other service classes. DbContexts or DbContextFactories will get their connection strings within the static Main method, where you build your IConfiguration to go into the IServiceProvider and can pull connections strings at the same time.

Is each DataAccess the same DbContext or are there different tables, etc., involved? If they are the same, create a projects for your DAL from which each of your other projects can depend.

How do I get the connection string from appsettings.json into a static class, where I can't call a constructor?

You need to initialize the configuration when the app starts. Refer to the following sample:

In the appsettings.json file, there have a connection string:

{
  "ConnectionStrings": {
    "DefaultConnection": "......"
  },

Create a static class AppSettings.cs:

public static class AppSettings
{
    public static string? ConnectionString { get; private set; }

    public static void Initialize(IConfiguration configuration)
    {
        ConnectionString = configuration.GetConnectionString("DefaultConnection");
    }
    public static string GetConnectionString()
    {
        return ConnectionString;
    }
}

Then, initialize the AppSettings in the Program.cs:

    var builder = WebApplication.CreateBuilder(args);
    ...
    // Initialize AppSettings
    AppSettings.Initialize(builder.Configuration);

    var app = builder.Build();
    ...

After that you can access connection string use the following code:

    public IActionResult Index()
    {
        var connection = AppSettings.ConnectionString;
        var connection2 = AppSettings.GetConnectionString();
        return View();
    }

The result as below:

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论