Reputation: 9378
I have an asp.net application. I have two databases set up, a test and production. Write now before I push any updates I manually change my conn string to point to production. Is there a way to do this when I publish my application, automatically change to production? Thanks for any help.
Upvotes: 1
Views: 2393
Reputation: 1
Yeah web.config transformations or I think a better approach is to separate your configs from your web/app.config, so having a folder for each build configuration for example
Config/Debug/connectionStrings.config
Config/Stage/connectionStrings.config
Config/Production/connectionStrings.config
Then your app or web.config would look like
<connectionStrings configSource="bin\connectionStrings.config" />
To get the environment specific config to your bin directory create a post build event which copies the config based on the current build configuration or copy them manually to your bin. This way you don't have to relay on a build to get your desired configuration.
If you really want this automated your post build event could look something like
"$(SolutionDir)CopyConfigs.bat" "$(ProjectDir)" "$(ConfigurationName)" "$(OutDir).."
and the batch file so the copying can be reused between projects:
@echo CopyConfigs.bat :
@echo Coping Config Files...
set projectDir=%1
set configurationName=%2
set outDir=%3
REM Trim Quotes
for /f "useback tokens=*" %%a in ('%1') do set projectDir=%%~a
for /f "useback tokens=*" %%a in ('%2') do set configurationName=%%~a
for /f "useback tokens=*" %%a in ('%3') do set str3=%%~a
@echo Project Directory: %1
@echo ConfigurationName: %2
@echo OutDir: %3
if not exist %1 goto ProjectDirectoryNotFound
REM Copy the configuration files to the projects output directory
xcopy /Y "%projectDir%Configuration\%configurationName%\*.config" "%outDir%"
xcopy /Y "%projectDir%Configuration\*.config" "%outDir%"
@goto END
:ProjectDirectoryNotFound
@echo Project Directory %projectDir% was not found.
@goto END
:END
@echo Coping Config Done
Upvotes: 0