2016-08-02 5 views
0

MVCアプリケーションを作成しています。エンティティフレームワークのDb接続文字列を使用するAppsettings.configとEntityフレームワークの接続がweb.configで自動的に生成されます。私は、サーバーにコードを移動するために計画された後、私は、サーバーのDBは、私がコメントローカルDB接続文字列のコードでしたい場合は、私が使用することができたい1とき、私は、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;" /> 

接続文字列と同様にサーバーの詳細を持っています。 ウェブ設定エンティティフレームワークコード:

<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> 

私はウェブの設定コードを変更することができますどのようにサーバーのコードを移動?私は同じコードを使用する必要がありますか他のサーバーの詳細で使用する必要がありますか?どのようにアプリケーションを公開した後、このコードサーバーのWeb設定を移動するには?

+0

あなたはサーバーデータベースの 'username'と' password'を含めると思いますか? – jonju

+0

@jonju Web設定の接続文字列にサーバーのユーザー名とサーバーのパスワードを含める必要がありますか? – TickTech

+0

サーバー接続でプロジェクトを公開しても、サーバーのprojectForderにアクセスして、新しい接続でwebConfigを編集することができます。 PS。実際のパスワードで接続文字列を絶対に投稿しないでください! –

答えて

1

ターゲット環境に応じて接続文字列を変更するには、web.config transforを使用できます。

ここでは、リンク

そして、ここに私のプロジェクトの一つで、変換XMLの例のカップルです:

<?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> 

として、あなたは見ることができます。変換は元の宣言と非常に似ていますが、変換処理を定義する2つの付加的な属性(xdt:Transformおよびxdt:Locator)があります。

+0

私はこれを試して、 @mnieto – TickTech

関連する問題