简体   繁体   中英

How to pass connection string to a class library?

I have a project structure like

MySolution
    -FrontEnd
         -webforms
         -console
         -Mvc
    -Repositories
         -Dapper   (Class library)
    -Tests

How do I reference the connection string in my repository class library? I tried adding App.Config in my class library project but it is not available in Add New Items. I am using Visual Studio 2013 express for web. Thanks, Damien.

The .Config that applies to libraries is the one of the executing assembly using those libraries, so you don't have to add an App.Config to your project, it will use the App or Web.Config of your startup project.

In order to get access to the ConfigurationManager you need to add a reference to System.Configuration in your library project.

Use web.config in the FrontEnd project. Do not use app.config in the Repositories project.

This article explains how to read the value from web.config.

http://www.connectionstrings.com/store-connection-string-in-webconfig/

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