Passing application's connection string down to a Repository Class Library in ASP.NET 5 using the IConfigurationRoot

后端 未结 7 1813
情话喂你
情话喂你 2020-12-08 19:51

I have an ASP.NET 5 MVC Web Application and in Startup.cs I see that the public property

IConfigurationRoot Configuration 

is being set to

7条回答
  •  执笔经年
    2020-12-08 20:31

    I have a constructor in my repository class that accepts the db connection string as a parameter. This works for me when I add my repository for injection. In ConfigureServies() of the startup.cs file add this:

    services.AddScoped(c => new Repos(Configuration["DbConnections:ConnStr1"]));
    

    IRepos.cs is the interface, Repos.cs is the class that implements it. And of course Configuration is just a reference to the built IConfigurationRoot object.

提交回复
热议问题