0

I am creating a MVC application. Using entity framework Db connection string included Appsettings.config and Entity framework connection auto generated in web.config its Working fine. After i planned to move the code to server i have server details Like

Connection string in Appsettings

Server Database :
 <add key="thenna" value="server=123.45.45.34;database=montage;user  id=*****;password=*******;trusted_connection=false;"/>  

 Local Database:
<add key="thenna" value="server=DESKTOP-QD6A981\SQLEXPRESS;database=tickandtie;user id=***;password=*****;trusted_connection=false;" />

when i which one i want i can use if server db want i was comment local db connection string code . web config entity framework Code :

<connectionStrings><add name="tickandtieEntities" connectionString="metadata=res://*/Entityframework.Tickmarks.csdl|res://*/Entityframework.Tickmarks.ssdl|res://*/Entityframework.Tickmarks.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DESKTOP-QD6A981\SQLEXPRESS;initial catalog=tickandtie;user id=sa;password=Tickmarks;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" /></connectionStrings>

when i move server code how can i change Web config code ? i need to use Same code or else i need use with server details? how to move this code server web config after publish the application ?

7
  • Are you suppose to include the username and the password of the server database? Commented Aug 2, 2016 at 10:38
  • @jonju i need include server username and server password in web config connection string? Commented Aug 2, 2016 at 10:42
  • even you publish the project with the server connection, or you can go on the server projectForder, and edit the webConfig with new connection. PS. Never post the connection string with real password! Commented Aug 2, 2016 at 10:45
  • No what I meant was aren't your username and password supposed to be secret? You are showing it to the WORLD Commented Aug 2, 2016 at 10:45
  • @jonju changed server name username and password dummy Commented Aug 2, 2016 at 10:48

1 Answer 1

1

you can use web.config transfor to change the connection string depending on your target environment.

Here you are a couple of links

And here an example of the transformation xml in one of my projects:

<?xml version="1.0" encoding="utf-8" ?>
<!-- For more information on using transformations 
     see the web.config examples at http://go.microsoft.com/fwlink/?LinkId=214134. -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <connectionStrings>
    <add name="OASIS" 
         connectionString="Data source=server\instance; Initial catalog=dbname; User Id=user;Password=pass;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" 
         xdt:Transform="SetAttributes" xdt:Locator="Match(name)"
         />
  </connectionStrings>

</configuration>

As you can see. the transform is very similar to the original declaration, but with 2 aditional attributes (xdt:Transform and xdt:Locator) that define the transformation proccess.

Sign up to request clarification or add additional context in comments.

1 Comment

i will try this and get you back @mnieto

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.