Connectionstring according to scenario

Asked

Viewed 59 times

1

Hello, good morning!

I have a project where there are two scenarios, which is approval and production. In this same project I created two connectionstrings, setting one for each scenario.

In the system these scenarios are divided into subdomains, such as Homolog.dominio.com.br and the domain.com.br itself.

Now comes the question: There is a way to create a configuration for the connectionstring adapt to the scenario? When I access the Homolog it automatically picks up the connectionstring of approval.

It’s like this:

<connectionStrings>
    <add name="connectionstring_homolog" providerName="System.Data.SqlClient" connectionString="Data Source=xxxx;Initial Catalog=xxxxxx; User ID=xxxxxx; pwd=xxxxxxx;" />
    <add name="connectionstring_prod" providerName="System.Data.SqlClient" connectionString="Data Source=xxxxxx;Initial Catalog=xxxxxx; User ID=xxxx; pwd=xxxxx;" />
  </connectionStrings>

Remembering that I am using ASP.Net MVC.

Thank you in advance.

  • You can set up web.config to swap with Debug and Release. ?

  • Hello Jhonathan, I am trying to do through a session, do not know if it is a good practice, more how would that idea? Could I explain? Thank you.

1 answer

2

First you should look inside your application on the web.Config

In the web.config section put the following code:

  <connectionStrings configSource="WebConnStrDevelopment.config">
    <!-- @configSource será modificado na publicação. Ver Web.Release.config -->
  </connectionStrings>

Now in your Web.Release.config add the following code:

 <connectionStrings configSource="WebConnStrProduction.config" xdt:Transform="SetAttributes(configSource)" />

Now add 2 new config files called (Webconnstrdevelopment and Webconnstrproduction)

Inside the Webconnstrdevelopment.config file add the following code:

    <connectionStrings>
    <add name="connectionstring"  //adicionar conexão de dev />
  </connectionStrings>

Inside the Webconnstrproduction.config file add the following code:

      <connectionStrings>
    <add name="connectionstring"  //adicionar conexão de prod/>
  </connectionStrings>

You can read more here on the MSDN

  • I’ll check, thank you very much Jhonathan!

  • @Bruno When publishing or debugging local, just switch from Debug to Release to switch between the connectivions

Browser other questions tagged

You are not signed in. Login or sign up in order to post.