User 1986
User 1986

Reputation: 11

Getting an error 'Couldn't load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral' or one of its dependencies.'

Please note am using VS 2017, .Net framework 4.6.1, and entity framework version 6.4.0. Am getting an error when I try to run unit test project, and error as shown below,

'Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified.'

Please note I already tried below solutions,

  1. Uninstalled EF version 6.4.0 and installed 6.0.0
  2. Changed EF version in app.config <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/> to <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
  3. Set EF Reference property 'Specific Version' to false

By trying the above solutions still I issue did not get resolved.

Also, please note I tried based on the answers provided in the Stack over flow for previously asked queries about the same issue.

App.config file,

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
  <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --><!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <!--<section name="entityFramework"
             type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,
             EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
             requirePermission="false"/>-->
    <section name="entityFramework"
             type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,
                    EntityFramework, Culture=neutral, PublicKeyToken=b77a5c561934e089"
             requirePermission="false"/>
  </configSections>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
    </providers>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v13.0"/>
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <connectionStrings>
    <add name="SOwDatabaseDbContext" connectionString="Data Source=ABC;Initial Catalog=SOW;Integrated Security=True"
      providerName="System.Data.SqlClient"/>
  </connectionStrings>
</configuration>

Package.config file,

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="6.4.0" targetFramework="net461" />
  <package id="EntityFramework.Core" version="7.0.0-beta6" targetFramework="net461" />
  <package id="EntityFramework.Relational" version="7.0.0-beta6" targetFramework="net461" />
  <package id="Ix-Async" version="1.2.4" targetFramework="net461" />
  <package id="Microsoft.Framework.Caching.Abstractions" version="1.0.0-beta6" targetFramework="net461" />
  <package id="Microsoft.Framework.Caching.Memory" version="1.0.0-beta6" targetFramework="net461" />
  <package id="Microsoft.Framework.Configuration" version="1.0.0-beta6" targetFramework="net461" />
  <package id="Microsoft.Framework.Configuration.Abstractions" version="1.0.0-beta6" targetFramework="net461" />
  <package id="Microsoft.Framework.Configuration.Binder" version="1.0.0-beta6" targetFramework="net461" />
  <package id="Microsoft.Framework.DependencyInjection" version="1.0.0-beta6" targetFramework="net461" />
  <package id="Microsoft.Framework.DependencyInjection.Abstractions" version="1.0.0-beta6" targetFramework="net461" />
  <package id="Microsoft.Framework.Logging" version="1.0.0-beta6" targetFramework="net461" />
  <package id="Microsoft.Framework.Logging.Abstractions" version="1.0.0-beta6" targetFramework="net461" />
  <package id="Microsoft.Framework.OptionsModel" version="1.0.0-beta6" targetFramework="net461" />
  <package id="Remotion.Linq" version="2.0.0-alpha-004" targetFramework="net461" />
  <package id="System.Collections" version="4.0.0-beta-23109" targetFramework="net461" />
  <package id="System.Collections.Immutable" version="1.1.37-beta-23109" targetFramework="net461" />
  <package id="System.Diagnostics.Debug" version="4.0.0-beta-23109" targetFramework="net461" />
  <package id="System.Globalization" version="4.0.0-beta-23109" targetFramework="net461" />
  <package id="System.Linq" version="4.0.0-beta-23109" targetFramework="net461" />
  <package id="System.Resources.ResourceManager" version="4.0.0-beta-23109" targetFramework="net461" />
  <package id="System.Runtime" version="4.0.0-beta-23109" targetFramework="net461" />
  <package id="System.Runtime.Extensions" version="4.0.0-beta-23109" targetFramework="net461" />
  <package id="System.Threading" version="4.0.0-beta-23109" targetFramework="net461" />
</packages>

UnitTest class file (Please note to pin point the issue related to EF I did write unit test cases directly for repository class)

TestMethod]
       public void InsertCategoryTest()
        {
            try
            {
                CategoriesRepository categoriesRepository = new CategoriesRepository(); // Throwing exception here
                Category categoryModel = new Category()
                {
                    CategoryName = "Test"
                };
                bool res = categoriesRepository.InsertCategory(categoryModel);
                Assert.AreEqual(res, true);
            }
            catch(Exception ex)
            {

            }

Any suggestion will be appreciated.

Upvotes: 0

Views: 2300

Answers (1)

Steve Py
Steve Py

Reputation: 34653

This can happen for a number of reasons such as changing versions of EF dependencies in a project or installing assemblies that have dependencies on EF. This can be due to a mismatch in EF versions (without a configured version override) or having an incorrectly configured override left within the configuration.

The first thing to check in Visual Studio is to select the Solution in the solution explorer and in the right-click context menu select "Manage Nuget Packages for Solution..." From here, select the "Installed" heading and find "EntityFramework". On the right-hand side it will list what assemblies in your solution are using EF and what version. The smoking gun is where it says: "Installed: [ ***** ]" If it says "multiple versions installed" then you have assemblies referencing different versions. As you scroll through the list you may see some using 6.4 and some using 6.0. If you want to use 6.4 for all, tick the projects that say 6.0, then be sure that 6.4 is selected from the "Version:" drop down and select "Install".

Alternatively you can check your test project, and any application project's app.config/web.config file for assembly bindings. If you have a common DLL that is set up for 6.0 and it is used by older projects running 6.0, but you also want to use it in a new project running 6.4 then you can use a <dependentAssembly> mapping in the new project to map any earlier assembly references to 6.4

It would look something like:

<dependentAssembly>
    <assemblyIdentity name="EntityFramework" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
    <bindingRedirect oldVersion="0.0.0.0-6.4.0.0" newVersion="6.4.0.0" />
</dependentAssembly>

**edit: Note that the publicKeyToken value above was not taken from EF, but a different assembly reference I had. If you go to add a dependent assembly entry for EF you will need to check the valid publicKeyToken for EF. I didn't have an example on hand for it.

If all of your projects are pointing to 6.4 and you are still getting a reference error to 6.0, still check whether you have a <dependentAssembly> reference in any of the projects and check for any that might have a newVersion="6.0.0.0" setting. Sometimes when installing and upgrading dependencies, these dependent assembly references get initially added, but then are not removed. Occasionally I have come across this issue when upgrading assembly references.

Upvotes: 1

Related Questions