6

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.

4
  • This might help - forums.asp.net/t/1394239.aspx Commented Jan 27, 2014 at 6:49
  • It's strange, check for Application Configuration File in "Add new item..." menu again. If there is none, create it manually and then include in project. Also, this question might be useful Commented Jan 27, 2014 at 7:03
  • @Aishvarya that does not help me as I cannot access ConfigurationManager inside my Class Library maybe because I have started from a Blank application project. Commented Jan 27, 2014 at 7:10
  • I got this answer for more or less the same question: stackoverflow.com/questions/5894153/… Commented Jan 27, 2014 at 8:20

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, for some odd reason I could not reference using ConfigurationManager, however it worked using WebConfigurationManager.
3

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/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.