'System.Data.Entity.Internal.AppConfig'的类型初始值设定项引发了异常

时间:2013-07-11 05:23:53

标签: asp.net asp.net-mvc entity-framework asp.net-mvc-4 entity-framework-5

我在MVC 4 aspnet网站上使用EF5。 在本地,一切工作正常,但当我将其发布到IIS并尝试进入时,我收到错误

  

“System.Data.Entity.Internal.AppConfig”的类型初始值设定项   抛出一个例外。“

详细例外

  

为其创建配置节处理程序时发生错误   entityFramework:DbContext类型的配置   “GdpSoftware.Server.Data.GdpSoftwareDbContext,   GdpSoftware.Server.Data'在。中多次指定   应用配置。每个上下文只能配置一次。   (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>

26 个答案:

答案 0 :(得分:73)

在App.config文件中执行以下操作,

  1. connectionStrings元素放在configSections元素之后。
  2. startup元素放在connectionStrings元素之后。
  3. <?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>
    

答案 1 :(得分:23)

我需要将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

答案 2 :(得分:4)

我发现删除对Entity Framework的引用并从NuGet安装最新版本的Entity Framework解决了这个问题。它会在安装过程中为您重新创建所有必需的条目。

答案 3 :(得分:3)

我对这个问题已经不知所措了,最后这对我有用: -

步骤1:使用Nuget包管理器

卸载实体框架

步骤2:从App.config中删除Entityframework元素

步骤3:重新安装所需版本的Entity Framework。

步骤4:删除迁移表和迁移文件夹。

步骤5:启用迁移并添加迁移和更新数据库

答案 4 :(得分:2)

我在web.config中指定了多个provider

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

我只是删除其中一个而且它有效。

我使用的是MySQL,而不是TSQL

答案 5 :(得分:1)

就我而言

TextFormField(
              keyboardType: TextInputType.number,
              inputFormatters: [
                LengthLimitingTextInputFormatter(3),
                WhitelistingTextInputFormatter.digitsOnly,
              ],
              decoration: const InputDecoration(
                labelText: 'reps',
              ),
              initialValue: widget.set.numberOfPets.toString() ,
              validator: (value) {
                return StringFieldValidator.validate(value);
              },
              onChanged: (value) {
                setState(() {
                  widget.set.numberOfPets= int.parse(value);
                });
              },
            ),

必须从版本4更新到6。我的意思是从EntityFramework更新了一个项目 4至6,但web.config未更新。

答案 6 :(得分:1)

根据下面的代码在Web.config中更改此部分。(尝试一下,对我有用。)

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="Data Source=.; Integrated Security=True; MultipleActiveResultSets=True" />   
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>

答案 7 :(得分:1)

当我忘记将Connections.config文件设置为“始终复制”

时,我遇到了这个问题

BareMessage =“无法打开configSource文件'Connections.config'。”

答案 8 :(得分:1)

我也遇到同样的问题,但是就我而言,我的解决方案有一个控制台应用程序和主要与数据库交互的EF类库。我从控制台应用程序配置中删除了与EF相关的配置设置。我在EF类库中仅维护了以下配置设置。

这对我有用。


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

                           


答案 9 :(得分:0)

我在Project Cms中有重复的连接字符串定义,并且Context类名为:CmsContext

对于我来说,问题得到了解决,因为我按如下所示更改了Web.config中的连接设置:第一个名称为CmsContext且与主项目有关。第二个名称为DefaultConnection且与身份有关

<add name="CmsContext" providerName="System.Data.SqlClient" connectionString="Data Source=DESKTOP-2NQSP1P\SQLEXPRESS;  Initial Catalog=CmsDB;Integrated Security=True;" />
</connectionStrings>

答案 10 :(得分:0)

我认为问题出在这一行:

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

我不知道你为什么使用这种方法以及它是如何工作的......

也许最好尝试从web.config中取出它并转向另一种方式

答案 11 :(得分:0)

这个老问题,但是由于其他答案中提到的不同原因,它才刚发生在我身上,所以我想分享一下我的发现。

就我而言,问题是我如何在app.config中定义连接字符串条目。我直接在Notepad++中对其进行了编辑,我必须找到一个快捷方式,并将未发现的整个字符串最小化,然后开始出现此错误。这是我的定义方式:

<add name="MyDb" connectionstring="data source=MyServer;
 initial catalog=MyDatabase;integrated security=true" 
 providername="System.Data.SqlClient" />

第二次我更改了以下参数

connectionstring to connectionString

providername to providerName

注意:该问题立即在Visual Studio中突出显示,但是如果您在客户端站点上,显然这不是理想的选择!

答案 12 :(得分:0)

我解决了这个问题,在文件machine.config的system.data部分中删除了DbproviderFactory,安装fbclient.dll时出现了一些脏数据。

C:\ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319 \ Config \ machine.config C:\ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ Config \ machine.config

  <system.data>
    <!-- <DbProviderFactories><add name="FirebirdClient Data Provider" invariant="FirebirdSql.Data.FirebirdClient" description=".NET Framework Data Provider for Firebird" type="FirebirdSql.Data.FirebirdClient.FirebirdClientFactory, FirebirdSql.Data.FirebirdClient, Version=4.10.0.0, Culture=neutral, PublicKeyToken=3750abcc3150b00c"/><add name="FirebirdClient Data Provider" invariant="FirebirdSql.Data.FirebirdClient" description=".NET Framework Data Provider for Firebird" type="FirebirdSql.Data.FirebirdClient.FirebirdClientFactory, FirebirdSql.Data.FirebirdClient, Version=6.4.0.0, Culture=neutral, PublicKeyToken=3750abcc3150b00c"/> -->
  </system.data>

答案 13 :(得分: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://*/ ...

因此删除不需要的连接字符串解决了我的问题。

答案 14 :(得分:0)

在我的情况下,在<clear />之后添加<connectionStrings>就像是魅力

答案 15 :(得分:0)

在连接字符串中,第一个字符串是web.config的基础

SchedulingContext 是实体文件的基本参数。

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

答案 16 :(得分:0)

当你像我一样在Nuget包中使用不同版本的EntityFramework版本时,会发生这种奇怪的错误。

首先,从NuGet包卸载您的Entity Framework DLL,然后清理app.config。通过从configSections和实体框架元素中删除条目。

接下来,安装所需的版本。这应该可以解决问题。

答案 17 :(得分:0)

一般问题是涉及机器/ Web / App配置的任何问题。

我在我的App.Config中的Machine.Config中有相同的连接字符串,所以我放在我的App.Config中的第一个连接字符串之前

答案 18 :(得分:0)

实际错误抛出消息= web.config中无法识别的元素'providers' 所以从web.config文件中删除提供者部分

答案 19 :(得分:0)

我遇到了类似的问题,将defaultConnectionFactory更改为SqlConnectionFactory帮助我解决了这个问题。

答案 20 :(得分:0)

如果您使用ASP.NET和IISExpress,请转到“C:\ Users \\ Documents \ IISExpress \ config \ applicationhost.config”,搜索您的项目并查看您是否有错误的virtualDirectory条目。

答案 21 :(得分:0)

我今天在另一个MVC应用程序中作为虚拟文件夹运行的嵌套MVC应用程序上遇到此错误。在我的例子中,InnerException比主要信息更丰富。它说:

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

在嵌套应用程序中修复重复的连接字符串后,一切正常。

答案 22 :(得分:0)

我有同样的问题。一天后,我明白了。

问题是在smtp下的mailSettings中添加了两个<system.net>代码。

答案 23 :(得分:0)

web.configApp.Config中搜索您是否有某些标签未在您的项目中使用,或者您的参考文献中没有。

答案 24 :(得分:0)

如果要为app.config维护另一个配置文件,请不要在父页面中包含任何键。

父页面: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>

答案 25 :(得分:-1)

检查C:\ inetpub \ wwwroot中的C驱动器,并从其中删除所有不必要的文件夹,否则从IIS中删除不必要的主机文件或文件夹。