web-dev-qa-db-ja.com

'System.Data.Entity.Internal.AppConfig'の型初期化子が例外をスローしました

MVC 4 aspnet WebサイトでEF5を使用しています。ローカルでは、すべて正常に機能しますが、IISに公開して入力しようとすると、エラーが発生します

「「System.Data.Entity.Internal.AppConfig」の型初期化子が例外をスローしました。」

詳細な例外

EntityFrameworkの構成セクションハンドラーの作成中にエラーが発生しました:アプリケーション構成でDbContextタイプ 'GdpSoftware.Server.Data.GdpSoftwareDbContext、GdpSoftware.Server.Data'の構成が複数回指定されています。各コンテキストは1回のみ設定できます。 (E:\ App\web.config行104)

StackOverflowで前の質問を確認し、Nuget EntityFrameworkを使用して既にアンインストールおよび再インストールし、各プロジェクトでの各参照がEF5であることを確認しました。また、各プロジェクトで選択したフレームワークが4.5であることも確認しました。

どんな考え、問題の原因は何ですか?ありがとう!ギジェルモ。

web.config

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.Microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />

  <!-- For more information on Entity Framework configuration, visit http://go.Microsoft.com/fwlink/?LinkID=237468 --></configSections>
  <connectionStrings>
    <add name="GdpSoftwareConnectionString" connectionString="Persist Security Info=False;User ID=user;Password=password;Initial Catalog=databasename;Data Source=server" providerName="System.Data.SqlClient" />
    <add name="GdpSoftware.Server.Data.GdpSoftwareDbContext" connectionString="GdpSoftware.Server.Data.GdpSoftwareDbContext_ConnectionString" providerName="System.Data.SqlClient"/>
    <add name="GdpSoftware.Server.Ui.Web.Models.UsersContext" connectionString="GdpSoftware.Server.Ui.Web.Models.UsersContext_ConnectionString" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Optimization" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
    <!-- BEGIN - TO SEE THE ERRORS ON THE DEPLOYMENT-->
    <customErrors mode="Off" />
    <!-- END - TO SEE THE ERRORS ON THE DEPLOYMENT-->
  </system.web>
  <system.webServer>
    <!-- BEGIN - TO SEE THE ERRORS ON THE DEPLOYMENT-->
    <httpErrors errorMode="Detailed" />
    <asp scriptErrorSentToBrowser="true" />
    <!-- END - TO SEE THE ERRORS ON THE DEPLOYMENT-->
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
      <remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
      <remove name="ExtensionlessUrlHandler-Integrated-4.0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
    <staticContent>
      <mimeMap fileExtension=".mustache" mimeType="text/plain" />
    </staticContent>
    <security>
      <requestFiltering>
        <fileExtensions>
          <add fileExtension=".mustache" allowed="true" />
        </fileExtensions>
      </requestFiltering>
    </security>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-Microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Castle.Windsor" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.2.0.0" newVersion="3.2.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.5.0.0" newVersion="4.5.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Host.SystemWeb" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.0.0.0" newVersion="1.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Castle.Core" publicKeyToken="407dd0808d44fbdc" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.2.0.0" newVersion="3.2.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <contexts>
      <context type="GdpSoftware.Server.Data.GdpSoftwareDbContext, GdpSoftware.Server.Data" disableDatabaseInitialization="true">
        <databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[GdpSoftware.Server.Data.GdpSoftwareDbContext, GdpSoftware.Server.Data], [GdpSoftware.Server.Data.Migrations.Configuration, GdpSoftware.Server.Data]], EntityFramework" />
      </context>
    </contexts>
  </entityFramework>
</configuration>
39
polonskyg

DefaultConnectionFactoryをデフォルトではなくSqlConnectionFactoryに変更する必要がありました

<entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
    <parameters>
      <parameter value="<My Connection String>" />
    </parameters>
  </defaultConnectionFactory>
</entityFramework>

http://blogs.msdn.com/b/davidobando/archive/2012/08/14/changing-ef-s-default-provider-from-localdb-to-sql-server.aspx

24
Brian

App.configファイルで次を実行します。

  1. connectionStrings要素の後にconfigSections要素を配置します。
  2. startup要素の後にconnectionStrings要素を配置します。
<?xml version="1.0" encoding="utf-8"?>
 <configuration>
   <configSections>
         <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=4.3.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
   </configSections>
   <connectionStrings>
         <add name="SchedulingContext" connectionString="Data Source=XXX\SQL2008R2DEV;Initial Catalog=YYY;Persist Security Info=True;User ID=sa;Password=XXX"   providerName="System.Data.SqlClient"/>
   </connectionStrings>
   <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>       
 </configuration>
74
Diganta Kumar

Entity Frameworkへの参照を削除し、NuGetからEntity Frameworkの最新バージョンをインストールすると、問題が修正されることがわかりました。インストール中に必要なすべてのエントリが再作成されます。

4
Ben Pretorius

私はこの問題について頭を痛めました、そして、最終的にここに私のために働いたものがあります:-

ステップ1:Nugetパッケージマネージャーを使用してEntity Frameworkをアンインストールする

ステップ2:App.configからEntityframework要素を削除する

Step3:Entity Frameworkの目的のバージョンを再インストールします。

手順4:移行テーブルと移行フォルダーを削除します。

手順5:移行を有効にし、移行とデータベースを追加する

3
Golden Griffin

Web.configで複数のprovidersが指定されました。

 <providers>
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.6.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
    </providers>

そのうちの1つを削除するだけで機能しました。

ただし、TSQLではなくMySQLを使用しています

2
Luke

Connections.configファイルを「常にコピー」に設定するのを忘れたときにこの問題に遭遇しました

BareMessage = "configSourceファイル 'Connections.config'を開けません。"

1
Matt Knight

私も同じ問題に直面しましたが、私の場合、私のソリューションにはコンソールアプリケーションと、主にデータベースと対話するEFクラスライブラリがあります。コンソールアプリケーション構成からEFに関連する構成設定を削除しました。 EFクラスライブラリの1つの場所でのみ、次の構成設定を維持しました。

これは私のために働いた。


<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.2.61023.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<add name="EntityModel" connectionString="Server=Localhost\SQLEXPRESS;Database=SampleEntities;Trusted_Connection=True;" providerName="System.Data.EntityClient" />

今日、他のMVCアプリケーションで仮想フォルダーとして実行されているネストされたMVCアプリケーションでこのエラーが発生しました。私の場合、InnerExceptionはメインの例外よりも有益でした。それは述べていました:

- The entry 'DbContextMain' has already been added. (C:\inetpub\...\web.config line x)

ネストされたアプリの重複した接続文字列を修正した後、すべて正常に機能しました。

0
Stefan Michev

接続文字列では、最初の文字列はweb.configのベースです

SchedulingContextは、Entityファイルの基本パラメーターです。

<connectionStrings>
     <add name="SchedulingContext" connectionString="Data Source=XXX\SQL2008R2DEV;Initial Catalog=YYY;Persist Security Info=True;User ID=sa;Password=XXX"   providerName="System.Data.SqlClient"/>
0

同じ問題がありました。ある日、私はそれを得た。

問題は、2つのsmtpタグを<system.net>の下のmailSettingsに追加することによるものでした。

私の場合、<clear />の直後に<connectionStrings>を追加すると、チャームのように機能しました

0
bax 1188

一般的な問題は、Machine/Web/App構成に関係する問題です。

Machine.ConfigのApp.Configと同じ接続文字列があったため、App.Configの最初の接続文字列の前に配置しました

0
Paul Totzke

同様の問題に直面し、defaultConnectionFactoryをSqlConnectionFactoryに変更することで解決できました。

0

App.configの構成ファイルをもう1つ保持している場合は、親ページにキーを含めないでください。

親ページ:app.config

<appSettings configSource="appSettings.config">
    <add key="ClientSettings" value="venice" /> <!-- Don't add Key Here -->
</appSettings>

子ページ:appSettings.config

<appSettings>
  <add key="ClientSettings" value="venice"/> <!-- add Here -->
</appSettings>
0
Bala Kumar

私は問題がこの行からであると思います:

<context type="GdpSoftware.Server.Data.GdpSoftwareDbContext, GdpSoftware.Server.Data" disableDatabaseInitialization="true">

なぜこのアプローチを使用しているのか、どのように機能するのかはわかりません...

たぶん、web.configからそれを取り出して別の方法で試してみる方が良いでしょう

0
Amin Saqi

WCFサービスに接続文字列の重複した定義がありました。サービスをデバッグし、内部エラーメッセージを表示することができました(デフォルトでは表示されません):

ConfigurationErrorsException: The entry 'xxxEntities' has 
already been added. (C:\Users\WcfService\web.config line 35). 

web.config変換後(重複値に注意)

<connectionStrings>
    <add name="xxxEntities" connectionString="metadata=res://*/ ...
    <add name="xxxEntities" connectionString="metadata=res://*/ ...

したがって、不要な接続文字列を削除することで問題が解決しました。

0
Honza P.

実際のエラーがweb.configのMessage = Unrecognized要素 'providers'をスローしたため、web.configファイルからプロバイダセクションを削除します

0
Bala

プロジェクトで使用されていないタグがある場合、または参照に含まれていない場合は、web.configまたはApp.Configを検索してください。

0
Oswaldo Alvarez

この奇妙なエラーは、私が行ったようにNugetパッケージのEntityFrameworkバージョンのさまざまなバージョンをいじるときに発生します。

まず、NuGetパッケージからEntity Framework DLLをアンインストールしてから、app.configをクリーンアップします。 configSectionsおよびエンティティフレームワーク要素からエントリを削除する。

次に、目的のバージョンをインストールします。これで問題が解決するはずです。

0
Bharat Raj

ASP.NETとIISExpressを使用する場合は、「C:\ Users \\ Documents\IISExpress\config\applicationhost.config」に移動し、プロジェクトを検索して、virtualDirectoryエントリに問題がないかどうかを確認します。

0
malt