53

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

27 に答える 27

87

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>
于 2013-09-26T03:41:02.023 に答える
26

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/ching-ef-s-default-provider-from-localdb-to-sql-server.aspx

于 2013-11-01T21:41:46.803 に答える
6

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

于 2014-02-04T06:23:56.513 に答える
2

providerweb.config で複数の を指定しました。

 <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を使用しています

于 2015-05-25T13:58:04.307 に答える
1

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

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

于 2016-05-18T15:58:54.583 に答える
0

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

于 2014-11-21T19:47:55.773 に答える
0

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

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

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

于 2015-08-29T13:50:36.143 に答える
0

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

于 2015-10-28T07:43:29.803 に答える
0

問題は次の行にあると思います。

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

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

たぶん、web.configから取り出して、別の方法で行ったほうがよいでしょう

于 2013-07-30T10:13:40.560 に答える
0

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

于 2016-08-12T15:05:34.283 に答える
0

一般的な問題は、マシン/Web/アプリ構成に関連する問題です。

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

于 2017-02-22T20:31:58.030 に答える
0

私は同じ問題を抱えていました。1日後、私はそれを手に入れました。

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

于 2015-02-17T09:33:32.067 に答える
0

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://*/ ...

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

于 2019-06-26T07:40:44.017 に答える