问题
I have found that in the Machine.config you can add your connection strings so that you don't need your connection strings in each running apps config file. The down side to this is that you have number of framework versions * 2 machine.config file. So for version 4.* you have 2 alone. This took my research on to find that you can externalize the connection string using the configSource attribute in the connectionString element of the machine.config
<connectionStrings configSource="Config\connectionStrings.config" />
However, one realizes that the config source must be in a subfolder of where the the machine.config file is located. So how do you have one, machine wide, location to store all your connection strings?
回答1:
I couldn't find anywhere on the internet that had this answer. So I am going to answer this myself after some experimentation.
- Create a location and place your connectionstrings.config file in there. Ex. C:\Test\Connectionstrings.config
- In each of the machine.config locations. For example C:\Windows\Microsoft.net\Framework64\v4.*\Config. Do steps 3 and 4
- Open up command prompt as administrator to the the location and run the command:
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config>mklink /D Config C:\Test
- Edit the machine.config to be
<connectionStrings configSource="Config\connectionStrings.config" />
Now you have one location for all your connection strings.
来源:https://stackoverflow.com/questions/32400437/how-do-i-have-one-location-for-connection-strings-machine-wide