user516883
user516883

Reputation: 9378

Change connectionstring in Visual studio whening publishing

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

Answers (2)

Allan
Allan

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

Peter
Peter

Reputation: 12711

You can use web.config transformations for this.

Upvotes: 5

Related Questions