简体   繁体   中英

Change the connection strings in MVC3 to publish in IIS

I need to change the connection strings in my mvc proyect because is local and now I need publish in a server but I dont have idea how write the connection strings and they are the next

<add name="cnn" connectionString="Data Source=SISTEMAS-PC\SQLEXPRESS;Initial Catalog=FoodGroups;User ID=FoodGroup; Password=Food" providerName="System.Data.SqlClient" />
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(SISTEMAS-PC\SQLEXPRES)\v11.0;Initial Catalog=aspnet-MvcApplication1-20130730182253;Integrated Security=SSPI;AttachDBFilename=C:\Users\Sistemas\Desktop\proyectos TI\foodGroup2\foodGroup\App_Data" />
<add name="FoodGroupsEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=SISTEMAS-PC\SQLEXPRESS;initial catalog=FoodGroups;user id=FoodGroup;password=Food;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

You have a web.config which should have a section for all your connection string.

What you need to do, is to have a web.release.config that will change the connection string when you build in release mode.

For example, web.config when you are debugging in your dev environment:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
  <configSections>
 <connectionStrings>
      <add name="YourConnectionString"
           connectionString="Data Source=YourServer; initial catalog=YourDatabase; Integrated Security=True" providerName="System.Data.SqlClient" />
 </connectionStrings>
</configSections>

And inside the web.release.config

<?xml version="1.0"?>

    <connectionStrings>
      <add name="YourConnectionString" 
        connectionString="Data Source=ReleaseSQLServer;Initial Catalog=MyReleaseDB;Integrated Security=True"        xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
    </connectionStrings>

</configuration>

When you are building, you will see that the string "YourConnectionString" will be replaced. This way, you can deploy/publish your application depending of the build mode (debug or release) without having to modify your code and just relying on web.config.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM