Change the connection string from development to release upon publication

I want to know how to automatically change the connection string of my application, so when I work on it on my computer, it uses my local SQL Server, and as soon as I publish it, it uses SQL Server, which I posted on blue.

Now I just comment on the connection string depending on what I want, I saw that the Web.Config file has 2 dependencies, Web.Debug.config and Web.Release.config, where I think I have something to do but I don’t know that.

This is my web.config file so far

<connectionStrings>
    <add name="MyApp" connectionString="Data Source=mydb.database.windows.net;Initial Catalog=MyDb;User ID=MyUser@mydb.database.windows.net;Password=MyPwd;Encrypt=true;Trusted_Connection=false;" providerName="System.Data.SqlClient" />
  <!--<add name="MyApp" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Database=MyDb;Integrated Security=True;" />-->
  </connectionStrings

As you can see, I have two connection strings that I have, but this is really annoying.

+3
4

, dev Web.Debug.config prod- Web.Release.config. Release, Web.Config .

+2

web.config

  • web.dev.config
  • web.live.config

. , web.config .

web.config, web.config (dev, test, prod) codeproject

+2

I decided to go around the whole web.config and instead integrate a function that defines a connection string based on the current machine with which it is running. To establish this, I had to configure ApplicationDbContext () to collect the connection string from such a function:

public ApplicationDbContext() : base(CFFunctions.GetCFConnection())
{
}

Please note that my function is Static :

    public static string GetCFConnection()
    {
        string Connection = "";

        string Machine = System.Environment.MachineName.ToLower();

        switch(Machine)
        {
            case "development":
                Connection = @"Data Source=DEV_SRV;Initial Catalog=CeaseFire;Integrated Security=True"; 
                break;
            case "production":
                Connection = @"Data Source=PRO_SRV;Initial Catalog=CeaseFire;Integrated Security=True";
                break;
        }

        return Connection;
    }
0
source

Source: https://habr.com/ru/post/1674476/


All Articles