複数のデータベース接続と複数の web.config appSettings で動作する Entity Framework を使用して MVC 3 アプリケーションを取得する方法を知っている人はいますか?
2 に答える
1
サブフォルダー (site1、site2 など) が仮想ディレクトリであるかコントローラー名であるかに関係なく、それらの名前をさまざまな接続文字列に使用できます。
1 つの web.config ファイルで複数の接続文字列を使用し、要求された URL 仮想ディレクトリ名 (命名規則に基づく) でそれらを取得します。
web.config で:
<connectionStrings>
<add name="site1" connectionString="data source=BLAHBLAH1" />
<add name="site2" connectionString="data source=BLAHBLAH2" />
</connectionStrings>
とコードで:
using System.Configuration;
...
var currentLocationName = HttpRequest.ApplicationPath; // "site1, site2 etc."
string conn = ConfigurationManager.ConnectionStrings[currentLocationName].ConnectionString;
//set the correct connection string for your objectContext (Entity Framework)
return new Entities(conn);
于 2012-09-24T16:46:01.270 に答える
0
7 つの Web サイトがある場合、7 つの web.config ファイルもあり、それぞれが正しいデータベースを指します。
于 2012-09-24T16:45:43.547 に答える