如何在ASP.NET Core 3.1中的类库项目中获取连接字符串

发布于 2025-01-25 21:39:27 字数 94 浏览 6 评论 0原文

我在AppSettings.json中在我的ASP.NET Core Web API项目中从数据访问层中获取连接字符串。数据访问层中的项目是类库项目。我使用了DB的第一种方法

I'm having trouble getting the connection string in appsettings.json in my asp.net core web API project from the data access layer. The project in the data access layer is the class library project. I used DB first approach

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤痕我心 2025-02-01 21:39:27

在您的班级中注入“ IconFiguration”。请按照以下代码:

public class MyClassService: IMyClassService
{
    #region Fields
        
    private readonly IConfiguration _config;
    private string _connectionString => _config.GetConnectionString("DefaultConnection");
        
    #endregion

    #region Constructor
    
    public MyClassService(IConfiguration config)
    {
        _config = config;
               
    }
    
    #endregion
}

Inject 'IConfiguration' in your class. follow below code:

public class MyClassService: IMyClassService
{
    #region Fields
        
    private readonly IConfiguration _config;
    private string _connectionString => _config.GetConnectionString("DefaultConnection");
        
    #endregion

    #region Constructor
    
    public MyClassService(IConfiguration config)
    {
        _config = config;
               
    }
    
    #endregion
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文