Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'

I recently upgraded/updated Entity Framework in an old project from version 4 or 5 to version 6. Now I get this exception:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

Additional information: No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

I googled the error and came across a couple of SO threads, but none of them contained a solution that works for me. This is what my App.config looks like:

<?xml version="1.0" encoding="utf-8"?> <configuration>   <configSections>     <!-- 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" />   </configSections>   <entityFramework>     <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />     <providers>       <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />     </providers>   </entityFramework> </configuration> 

I already uninstalled Entity Framework from my project and re-installed it, deleted all the references to old EF files and re-installed, but nothing works for me. I keep getting this error.

like image 201
Leon Cullens Avatar asked Feb 08 '14 03:02

Leon Cullens


2 Answers

I had this problem in a situation where I have a model project that has the references to both EntityFramework and the .SqlServer assemblies, and a separate UI project that uses ASP.NET MVC. I wanted to upgrade from EF 4.1 to 6.1. I actually had to do part of what was described here: http://robsneuron.blogspot.com/2013/11/entity-framework-upgrade-to-6.html. I want to emphasize that I did not add a reference to these projects to my UI project nor did I add the configuration to the UI project's web.config, as those steps would violate my separation of concerns.

What I did do was in my model project, I had to flip the "Copy Local" reference settings for EntityFramework.SqlServer (and the EntityFramework reference, to be safe) to "False" and save all, to get the project to put the <Private> node into the .csproj file, and then set it back to "True" and save again, so that True ends up as the final value.

I also had to add the hack line to my DbContext-derived class in its constructor to force the use of the assembly, even though the line does nothing. Both of these steps I learned from the blog post.

public MyContext : DbContext {     public MyContext() : base("name=MyContext")     {         // the terrible hack         var ensureDLLIsCopied =                  System.Data.Entity.SqlServer.SqlProviderServices.Instance;        } 

I want to thank the author of that blog that I referenced, as well as all the people that asked and answered the questions on SO, to try to help us get past this terrible bug.

like image 168
bcr Avatar answered Sep 23 '22 19:09

bcr


Ok, this is pretty weird. I have a couple of projects: one is a UI project (an ASP.NET MVC project) and the others are projects for stuff like repositories. The repositories project had a reference to EF, but the UI project didn't (because it didn't need one, it just needed to reference the other projects). After I installed EF for the UI project as well, everything started working. This is why, it added this piece of code to my Web.config:

<entityFramework>   <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />   <providers>     <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />   </providers> </entityFramework> 
like image 43
Leon Cullens Avatar answered Sep 23 '22 19:09

Leon Cullens