There is a duplicate 'entityFramework' section defined - EntityFramework6 upgrade

前端 未结 4 1373
甜味超标
甜味超标 2020-12-19 13:11

I have recently updated EntityFramework in my WebAPI project from v5 to v6. Unfortunately it seems like somewhere, somehow, there is a reference that exists in some config s

相关标签:
4条回答
  • 2020-12-19 13:26

    I was running into the exact same problem with having a Virtual Directory running EF6 and the root directory running EF5. Took me a few hours but I found a fix:

    In the virtual directory Web.config, remove/comment out the configSection for EF6 (this stops you from duplicating the entityFramework section), add an assembly binding redirect (so it maps the root's EF5 configSection to EF6, but only in the Virtual Directory) and finally remove all the <entityFramework> configuration so it doesn't try to look for a configSection for EF6 in the Virtual Directory. My new Web.config file looks like this:

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

    Next I added a DbConfiguration class to the assembly that contains the DbContext (this basically enables the EF6 code first configuration). For example:

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

    And that was it, now I can have both EF versions working along each other. The tricky part for me was figuring out how to do the AssemblyBinding from EF5 to EF6 redirect. Also notice that I didn't change anything in the root Web.config.

    Hopefully this helps someone out there having the same issue.

    0 讨论(0)
  • 2020-12-19 13:26

    I know its a bit late to answer but I use S/O as a personal reference and I ran into this problem myself recently so I would like to offer a solution that worked for me.

    This is happening simply because you have 2 versions of EF installed. Simply go to the nugget package manager and uninstall the version you don't want (i.e. if you have 5 and 6 of EF, you might want to uninstall EF5). When complete a restart might be required but all should work after.

    Hope this helps. If you want me to explain in more details the step by step process just add a comment and let me know.

    0 讨论(0)
  • 2020-12-19 13:29

    Keeping the same entityFramework versions in both parent and virtual folder web configs fixes the issue for me.

    If you have an older version of the entityFramework installed in the project, right click on the project then go to "Manage NuGet Packages" and then select "search online" option from the left and select version you want and install it.

    0 讨论(0)
  • 2020-12-19 13:51

    The problem was related to the root directory of the site; I forgot it was pointing to another related project, with this project mapped to a virtual directory. This lead to the other web.config being loaded.

    0 讨论(0)
提交回复
热议问题