【问题标题】:There is a duplicate 'entityFramework' section defined - EntityFramework6 upgrade定义了一个重复的“实体框架”部分 - EntityFramework6 升级
【发布时间】:2013-10-11 17:50:12
【问题描述】:

我最近将我的 WebAPI 项目中的 EntityFramework 从 v5 更新到了 v6。不幸的是,它似乎在某个地方,不知何故,在某个设置为 v5.0 的某个配置中存在一个引用。我似乎找不到它;我将在下面发布我的 web.config。

出现的错误是:

定义了一个重复的“entityFramework”部分。

我找不到那个重复的引用,但是如果我删除了单个引用,我会收到一条错误消息:

{"Message":"发生错误。","ExceptionMessage":"无法加载文件或程序集 'EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' 或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。(来自 HRESULT 的异常:0x80131040)","ExceptionType":"System.IO.FileLoadException","StackTrace":" at System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost host, String typeString, Boolean throwOnError)\r\n 在 System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.Init(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord)\r\n 在 System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.InitWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord)\r \n 在 System.Configuration.RuntimeConfigurationRecord.CreateSectionFactory(FactoryRecord factoryRecord)\r\n 在 System.Configuration .BaseConfigurationRecord.FindAndEnsureFactoryRecord(String configKey, Boolean& isRootDeclaredHere)"}

这是我的 web.config:

<?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.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></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="None" />
        <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>
        <profile defaultProvider="DefaultProfileProvider">
          <providers>
            <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
          </providers>
        </profile>
        <membership defaultProvider="DefaultMembershipProvider">
          <providers>
            <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
          </providers>
        </membership>
        <roleManager defaultProvider="DefaultRoleProvider">
          <providers>
            <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
          </providers>
        </roleManager>
        <!--
                If you are deploying to a cloud environment that has multiple web server instances,
                you should change session state mode from "InProc" to "Custom". In addition,
                change the connection string named "DefaultConnection" to connect to an instance
                of SQL Server (including SQL Azure and SQL  Compact) instead of to SQL Server Express.
          -->
        <sessionState mode="InProc" customProvider="DefaultSessionProvider">
          <providers>
            <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
          </providers>
        </sessionState>
      </system.web>
      <system.webServer>
        <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>
      </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="1.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="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-1.3.0.0" newVersion="1.3.0.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Core.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="v11.0" />
          </parameters>
        </defaultConnectionFactory>
        <providers>
          <provider invariantName="System.Data.Entity.Core.SqlClient" type="System.Data.Entity.Core.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>
    </configuration>

【问题讨论】:

    标签: c# entity-framework


    【解决方案1】:

    我在虚拟目录运行 EF6 和根目录运行 EF5 时遇到了完全相同的问题。花了我几个小时,但我找到了解决办法:

    在虚拟目录 Web.config 中,删除/注释掉 EF6 的 configSection(这会阻止您复制 entityFramework 部分),添加程序集绑定重定向(因此它将根的 EF5 configSection 映射到 EF6,但仅在虚拟目录),最后删除所有 &lt;entityFramework&gt; 配置,因此它不会尝试在虚拟目录中查找 EF6 的 configSection。我的新 Web.config 文件如下所示:

    <configuration>
      <!--<configSections>
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.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>-->
    
    ... rest of configuration ...
    
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <dependentAssembly>
            <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="6.0.0.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
      <!--<entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True" />
          </parameters>
        </defaultConnectionFactory>
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>-->
    

    接下来,我向包含 DbContext 的程序集添加了一个 DbConfiguration 类(这基本上启用了 EF6 代码优先配置)。例如:

    public class MyConfiguration : DbConfiguration
    {
        public MyConfiguration()
        {
            SetDefaultConnectionFactory(new SqlConnectionFactory("Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True"));
        }
    }
    

    就是这样,现在我可以让两个 EF 版本相互配合。对我来说棘手的部分是弄清楚如何进行从 EF5 到 EF6 重定向的 AssemblyBinding。另请注意,我没有更改根 Web.config 中的任何内容。

    希望这可以帮助遇到同样问题的人。

    【讨论】:

    【解决方案2】:

    问题与网站根目录有关;我忘了它指向另一个相关项目,这个项目映射到一个虚拟目录。这会导致另一个 web.config 被加载。

    【讨论】:

    • 那是在 VS 解决方案还是项目中?您在哪里更改“站点的根目录”。我和你有同样的问题,任何额外的提示将不胜感激。
    • 基本上我一直在站点的虚拟目录中工作,因此根目录的web.config之间以及虚拟目录内部之间存在重复冲突。如果您使用的站点在多个目录中包含多个 asp.net 项目,请四处寻找其他 web.config
    • 我在 IIS Express 中遇到了这个问题,但无法确定在哪里可以找到父 web.config。所以...我只是让 Visual Studio 通过端口号重新映射到不同的目录。
    • 我在 IIS Express 中也遇到了这个问题。由于大约一年前的 TFS 问题,我已将我的项目复制到另一个本地 C: 文件夹。我没有意识到 IIS Express 仍然将该路径作为我站点的根路径,并且还有我的新位置。所以我不得不编辑 IIS Express 正在使用的站点的物理位置条目。我花了很长时间才找到 IIS Express 使用的配置文件。查看 C:\Users\yourUserIDHere\Documents\IISExpress\config\applicationhost.config。这就是我进行修复的地方。
    • 这太糟糕了。这意味着 ASP.NET MVC 项目对 root 和 VD 有不同的配置,VD 也依赖于 root。如果我出于某种原因重新部署 root,这会破坏 VD。糟糕的设计
    【解决方案3】:

    我知道回答有点晚了,但我使用 S/O 作为个人参考,最近我自己也遇到了这个问题,所以我想提供一个适合我的解决方案。

    发生这种情况仅仅是因为您安装了 2 个版本的 EF。只需转到 nugget 包管理器并卸载您不想要的版本(即,如果您有 5 和 6 的 EF,您可能需要卸载 EF5)。完成后可能需要重新启动,但之后一切正常。

    希望这会有所帮助。如果您希望我更详细地解释分步过程,只需添加评论并告诉我。

    【讨论】:

    • 很遗憾没有为我工作 :( 我们的 VS 解决方案只有新的 EF6 包,因为最近从 EF5 升级。将 web.config 中的 EF 版本从 6.0.0.0 更改为 5.0 .0.0 确实“修复”了它,但这是一个令人讨厌的临时破解。
    【解决方案4】:

    在父文件夹和虚拟文件夹 web 配置中保持相同的 entityFramework 版本可以解决我的问题。

    如果您在项目中安装了较旧版本的 entityFramework,请右键单击该项目,然后转到“管理 NuGet 包”,然后从左侧选择“在线搜索”选项并选择您想要的版本并安装它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-12
      • 2015-12-15
      • 1970-01-01
      • 2020-03-12
      • 1970-01-01
      • 2016-09-26
      相关资源
      最近更新 更多