Michael Michael - 1 month ago 15
C# Question

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?

Answer

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