Michael
Michael

Reputation: 749

How does ConfigurationManager.ConnectionStrings work?

So I have a Solution with 1 Class Library and 1 MVC project. The MVC project references the Class Library. I created a ConnectionString class in the Class library that looks like this:

public static string MyAppConnectionString { get; set; } =
                     ConfigurationManager.ConnectionStrings["MyApp"].ConnectionString; 

Everything is working fine. But what I don't understand is how ConfigurationManager.ConnectionStrings knows about the ConnectionStrings of BOTH the app.config file of the Class library AND the Web.Config file of the MVC project?

Upvotes: 0

Views: 2610

Answers (2)

Jose Francis
Jose Francis

Reputation: 960

The web.config file of the MVC app will be considered for ConfigurationManager and so your connection string has to be inside it.

Upvotes: 0

Jeffrey Patterson
Jeffrey Patterson

Reputation: 2562

ConfigurationManager works against the app.config file of the currently running executable, or in the case of web applications, against the web.config file. It does not use the app.config found in your library project.

https://msdn.microsoft.com/en-us/library/system.configuration.configurationmanager%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

Upvotes: 2

Related Questions