親ディレクトリで定義した接続文字列が有効になってしまう。 (構成にエラーがあります。 「パーサー エラー メッセージ: エントリ '(データベース接続文字列)' は既に追加されています。」が表示される)

ASP.NETアプリケーションを複数のディレクトリ階層設置した場合、親ディレクトリで定義した接続文字列が有効になってしまい定義が二重になっている旨のエラーが発生することがあります。
具体的なエラーメッセージとして、構成にエラーがあります。 「パーサー エラー メッセージ: エントリ '(データベース接続文字列)' は既に追加されています。」が表示されます。

対策方法は親フォルダのWeb.Configで定義された接続文字列を無効化することでエラーを回避できます。

エラー発生時の子ディレクトリのWeb.Config

<configuration>
  ......
  <system.web>
    ......
  </system.web>
  <connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=192.168.0.10;
      Initial Catalog=DBSample;Connect Timeout=60;Persist Security Info=True;
      User ID=sa;Password=abc123pass" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

対策後の子ディレクトリのWeb.Config

<clear /> タグを追加することで親ディレクトリのWeb.Configで定義した接続文字列をクリアできます。

<configuration>
  ......
  <system.web>
    ......
  </system.web>
  <connectionStrings>
    <clear />
    <add name="ConnectionString" connectionString="Data Source=192.168.0.10;
      Initial Catalog=DBSample;Connect Timeout=60;Persist Security Info=True;
      User ID=sa;Password=abc123pass" providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>


AuthorPortraitAlt
著者
iPentec.com の代表。ハードウェア、サーバー投資、管理などを担当。
Office 365やデータベースの記事なども担当。
作成日: 2011-01-30
Copyright © 1995–2025 iPentec all rights reserverd.