简体   繁体   中英

How to get connection string in class library project in asp.net core 3.1

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

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM