How to add config file to class library and then connection string for .NET Core 1.1

I am working on an n-tiered application where I have a Data Access Layer. which does not depend on other applications. I created the Class Library for .NET Core 1.1, I can see the dependencies folder but not the config / JSON file. I want to know if I can add an AppSetting.JSON file to my class library project? then add the connection string. I am aware to override ConfigureBuilder in class DbContext -> SQLServerConnection, but I want to save it in a separate config file and reference these connection strings.

It should also be noted that this class library will not directly reference the ASP.NET application

While searching on google, I found the following link https://www.stevejgordon.co.uk/project-json-replaced-by-csproj , but my question remains, where and how do I add the connection string for the Library project class?

+3


source to share


1 answer


You are kind of wrong. You don't want to have a config file for your DAL build, you just want your DAL to know nothing about the setup!

Typically, anything you configure for the DAL is a connector string, and this can easily be passed to the constructor:

public class MyRepository : IMyRepository
{
    public MyRepository(string connectionString){ ... }
}

      



When you use this DAL in something like webapi capabilities you will be using Dependency Injection - and here you will be reading the conn line from the web api config

public void ConfigureServices(IServiceCollection services)
{
    var connectionString = Configuration.GetConnectionString("MyConnectionString");
    services.AddScoped<IMyRepository>(sp => new MyRepository(connectionString));
}

      

+5


source







All Articles