C# 有一个重复的';实体框架&x27;已定义节-EntityFramework6升级

C# 有一个重复的';实体框架&x27;已定义节-EntityFramework6升级,c#,entity-framework,C#,Entity Framework,我最近在我的WebAPI项目中将EntityFramework从v5更新到了v6。不幸的是,它似乎在某个地方,不知何故,某个配置中存在一个设置为v5.0的引用。但我似乎找不到它;我将在下面发布我的web.config 显示的错误是: 定义了重复的“entityFramework”节 我找不到重复的引用,但如果我删除单个引用,则会出现一个错误,提示: {“Message”:“出现错误”。,“ExceptionMessage”:“无法加载文件或程序集”EntityFramework,版本=5.0.0

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

显示的错误是:

定义了重复的“entityFramework”节

我找不到重复的引用,但如果我删除单个引用,则会出现一个错误,提示:

{“Message”:“出现错误”。,“ExceptionMessage”:“无法加载文件或程序集”EntityFramework,版本=5.0.0.0,区域性=neutral,PublicKeyToken=b77a5c561934e089'或其依赖项之一。找到的程序集的清单定义与程序集引用不匹配。(HRESULT中的异常:0x8013100),“ExceptionType”:“System.IO.FileLoadException”、“StackTrace”:“at System.Configuration.TypeUtil.GetTypeWithReflectionPermission(IInternalConfigHost主机,字符串typeString,布尔throwOnError)\r\n at 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.FindAndensureRefactoryRecord(字符串configKey,布尔值&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>


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

我遇到了与运行EF6的虚拟目录和运行EF5的根目录完全相同的问题e几个小时后,我找到了一个解决方案:

在虚拟目录Web.config中,删除/注释掉EF6的configSection(这将阻止您复制entityFramework节),添加程序集绑定重定向(因此它将根目录的EF5 configSection映射到EF6,但仅在虚拟目录中)最后删除所有的
配置,这样它就不会试图在虚拟目录中查找EF6的configSection

<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>-->
就是这样,现在我可以让两个EF版本彼此协同工作。对我来说,棘手的部分是找出如何将AssemblyBinding从EF5重定向到EF6。还要注意,我没有在根Web.config中更改任何内容


希望这能帮助有同样问题的人。

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

发生这种情况的原因很简单,因为您安装了2个版本的EF。只需转到nugget软件包管理器并卸载您不需要的版本(即,如果您有5和6个EF,您可能希望卸载EF5)。完成后可能需要重新启动,但所有操作都应在安装后正常工作


希望这能有所帮助。如果您想让我更详细地解释分步过程,只需添加一条注释并让我知道。

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


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

这是在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项目具有不同的配置
public class MyConfiguration : DbConfiguration
{
    public MyConfiguration()
    {
        SetDefaultConnectionFactory(new SqlConnectionFactory("Data Source=(localdb)\v11.0; Integrated Security=True; MultipleActiveResultSets=True"));
    }
}