0

Using Visual Studio 2017, I have a solution with many C# projects in it. I'm trying to find a way to set a conditional compilation symbol in one location such that all projects will have access to it.

Mainly, this is because our solution can be deployed to one of two instances. Depending on where it is being deployed, the code will need to access a different Database (the two databases are basically the same, but one is our production DB and the other is for testing so we don't break things unintentionally). I'd like it if a programmer can just set a flag in one place, and have all projects then be able to connect to the appropriate DB in-code. I was planning on doing this by using a #if TestDB ... #else ... #endif when setting the connection strings.

I'd like to avoid using different Configurations (Release, Debug) due to some other flags that are already in use...

Any help would be appreciated!

Doc
  • 1,480
  • 2
  • 16
  • 28
  • Create a directory.props file and set a DefineConstants property with your symbols – pinkfloydx33 Jun 28 '18 at 23:40
  • You can [define them in your build script](https://stackoverflow.com/questions/479979/msbuild-defining-conditional-compilation-symbols) – John Wu Jun 29 '18 at 03:42
  • @pinkfloydx33 Can you provide a little more detail about this in an answer? Or a link to documentation for it? – Doc Jun 29 '18 at 03:42

1 Answers1

0

What type of solution is it? Web App/Windows App etc. I am not sure on why you dont want to use different configurations - if for example its a web app then you would have a web.config transform for each of the different instance you want to deploy too - and then set the connection string as required in the transform

Do you have a CI/CD Pipeline - if so it is perhaps something you could achieve with it with Powershell for example

Ctrl_Alt_Defeat
  • 3,933
  • 12
  • 66
  • 116
  • We aren't using Ci/CD unfortunately. We're dealing with Web Apps, but from my understanding of using the transforms of the web.config, that would require having different configurations to select the transform. Two problems: 1) you need a defined transform for every single project (of which there are many) and 2) I'm trying to avoid using build configurations. – Doc Jun 29 '18 at 03:41