Entity Framework 6 Weirdness

I used NuGet to install Entity Framework for a new project. To my surprise Entity Framework 6 was installed and version 5 is no longer available. The configuration file is certainly more complicated than it was in version 5. You are now required to have the entityFramework section in the application configuration file even if you are working with code-first.

So I was seeing the following error when running a test that inserted an entity into my database:

Unable to create instance of class PingYourPackage.Data.Tests.Repositories.And_Working_With_Roles. Error: System.InvalidOperationException: The Entity Framework provider type ‘System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer’ registered in the application config file for the ADO.NET provider with invariant name ‘System.Data.SqlClient’ could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information..

I went to the link in the error message and verified the configuration file was properly written during the NuGet package installation. I also saw that a reference had been added to EntityFramework.SqlServer.dll. I opened the assembly in Object Browser and verified that System.Data.Entity.SqlServer.SqlProviderServices was indeed present.

The stack trace was pretty deep, but it started in my DbContext derived class’s constructor:

at System.Data.Entity.DbContext..ctor(String nameOrConnectionString)
at MyAssembly.Data.MyDbContext..ctor()

I figured I try load the type myself so I put in this code in my DbContext derived class’s static constructor:

// Without this line, EF6 will break.
var type = typeof(System.Data.Entity.SqlServer.SqlProviderServices);

Problem solved.

My configuration file is shown below for reference. Note than with Entity Framework 5, I only needed the connection string section when working with code-first.

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