49

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.

Leon Cullens
  • 12,276
  • 10
  • 51
  • 85
  • so did you consider downgrading or you are trying to get that version to work – meda Feb 08 '14 at 08:26
  • EF6 has some features that are very useful for my project. – Leon Cullens Feb 08 '14 at 13:54
  • What version of visual studio do you have because I know 2010 was buggy with newer EF , I myself had an issue see [here](http://stackoverflow.com/questions/16928265/how-do-you-remove-old-version-of-entity-framework-in-gac) – meda Feb 08 '14 at 15:08
  • Visual Studio 2013 Premium. – Leon Cullens Feb 08 '14 at 16:10
  • 2
    Possible duplicate of [No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'](http://stackoverflow.com/questions/18455747/no-entity-framework-provider-found-for-the-ado-net-provider-with-invariant-name) – Rosberg Linhares Mar 01 '17 at 22:31

13 Answers13

51

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.

bcr
  • 1,983
  • 27
  • 30
  • 4
    I would upvote you twice if I could. Keeping the entity framework references and config sections out of the UI project is exactly what I was hoping for; thanks! – BCA Mar 20 '15 at 17:27
  • 2
    October, 2017. We have already landed on Mars, invented Viagra and are able to capture Pokemons with our mobile phones. Still our scientists, most brilliant of them, cannot solve the problem of copying DLL automatically. Thanks, @bcr, for your help :) – Dima Oct 31 '17 at 12:16
  • 2
    Jan 2019 and this is still a beautiful fix. Thanks for posting this. – Bob Black Jan 14 '19 at 23:10
  • Thank you for this. I realize I'm late to the show, but this resolved my issue. – ChettDM Jul 08 '19 at 15:06
36

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>
Leon Cullens
  • 12,276
  • 10
  • 51
  • 85
  • 5
    It's not weird, the app.configs of your dlls are not baked into the dll, there for if you need the config you need to add it to the main app/web.config. Don't forget, this also counts for connection strings – SynerCoder Feb 09 '14 at 13:54
  • I have same issue but NO SOLUTION :( – Neo Mar 09 '15 at 10:45
  • Thanks, fixed my problem as wel. Though we're working with SOC through a repository pattern. EF still had to be referenced in the web-project... – Rob Nov 14 '16 at 14:50
  • This pointed me in the right direction, but the other missing piece for me was manually adding a reference to `EntityFramework.SqlServer` in my project. This is all needed in EF6 *when you don't use NuGet* to configure it, see more in the [MSDN docs](https://msdn.microsoft.com/en-us/library/jj556606(v=vs.113).aspx). – Paul Dec 14 '17 at 07:13
  • This **fixed** my problem as well, but not sure it's an correct approach. Thanks anyway for the fix :) – Bhuwan Maharjan May 27 '18 at 02:20
16

I have the same problem, the difference is I don't have access to the source code. I've fixed my problem by putting correct version of EntityFramework.SqlServer.dll in the bin directory of the application.

hendryanw
  • 1,819
  • 5
  • 24
  • 39
13

I just solved it. You need to install Entity Framework again in your solution. Follow any of the approaches.

First = Right Click your Solution or Project root and click Manage NuGet Packages. Select 'EntityFramework', select the appropriate Projects and click Ok.

or

Second = Go to Console Package Manager and run Install-Package EntityFramework.

Hope it helps.

CAD bloke
  • 8,578
  • 7
  • 65
  • 114
Sandy
  • 11,332
  • 27
  • 76
  • 122
  • Right-click the Solution Root & manage the Nuget packages. easy.WIN. I added a project and it was missing from the new project, naturally. – CAD bloke Apr 17 '15 at 12:11
  • I got your point and its also correct. Improve my answer in case you wish. Thanks :) – Sandy Apr 17 '15 at 18:52
6

Hendry's answer is 100% correct. I had the same problem with my application, where there is repository project dealing with database with use of methods encapsulating EF db context operation. Other projects use this repository, and I don't want to reference EF in those projects. Somehow I don't feel it's proper, I need EF only in repository project. Anyway, copying EntityFramework.SqlServer.dll to other project output directory solves the problem. To avoid problems, when you forget to copy this dll, you can change repository build directory. Go to repository project's properties, select Build tab, and in output section you can set output directory to other project's build directory. Sure, it's just workaround. Maybe the hack, mentioned in some placec, is better:

var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance;

Still, for development purposes it is enough. Later, when preparing install or publish, you can add this file to package.

I'm quite new to EF. Is there any better method to solve this issue? I don't like "hack" - it makes me feel that there is something that is "not secure".

Mikk
  • 193
  • 3
  • 6
6

I have solved the issue using below code in my DBContext

 
public partial class Q4Sandbox : DbContext
    {
        public Q4Sandbox()
            : base("name=Q4Sandbox")
        {
        }

        public virtual DbSet Employees { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {           
           var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
        }
    }

Thanks to a SO member.

Aji
  • 193
  • 2
  • 6
  • Wait what, the code you provided does nothing to fix the error from the OP? You're not even using 'instance'. – Leon Cullens Feb 25 '16 at 23:36
  • 1
    Leon, I am using instance! – Aji Mar 01 '16 at 11:46
  • Did you try the source code? also have you encountered this issue? I have faced the issue and fixed it. Also for your question please check the code what I have written has suggested by microsoft – Aji Apr 12 '16 at 08:03
  • 1
    I just put this line (var instance = System....) in my DBContext class as shown here without modifying app.config or copy dll in the build folder and it works. For VB-programmers: "Dim instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance". Thank you very much. – Michael Bayer Apr 22 '16 at 13:57
  • Thanks Michael Bayer, please mark this as answer and upvote! :) – Aji Jul 12 '16 at 16:16
  • When a command provide you a solution it is not meaning less, it convert your code as meaning code also please check the microsoft website for better understanding – Aji Jul 29 '17 at 08:12
  • 1
    Thank you sir, that was missing in my code also. It seems that it cannot get the instance from the App.config file :) :) – Ange1 Aug 18 '17 at 09:51
3

Add "EntityFramework.SqlServer.dll" into your bin folder. Problem will get resolved.

vishal
  • 233
  • 4
  • 7
2

Try adding the following runtime assembly bindings:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
SynerCoder
  • 12,493
  • 4
  • 47
  • 78
  • Hmm, thanks to your answer I discovered something that was wrong. My ASP.NET project still had a reference to EF, but it was targetting EF5. I changed it to EF6, but that didn't fix the problem unfortunately. – Leon Cullens Feb 09 '14 at 02:03
2

I had the same problem and the only thing that works for my was to uninstall entity framework package from each project using Uninstall-Package. And then intall it again in the whole solution. It will ask you to choose in which project you want to install it, you shall select all.

tavo
  • 41
  • 2
2

What worked for me was downgrading from EF 6.1.3 to EF 6.1.1.

In Visual Studios 2012+ head over to:

Tools - Nuget Package Manager - Package Manager Console`

Then enter:

Install-Package EntityFramework.SqlServerCompact -Version 6.1.1

I did not have to uninstall EF 6.1.3 first because the command above already does that.

In addition, I don't know if it did something, but I also installed SQL Server CE in my project.

Here's the link to the solution I found:

http://www.itorian.com/2014/11/no-entity-framework-provider-found-for.html

Dayan
  • 7,634
  • 11
  • 49
  • 76
Omar
  • 21
  • 1
2

I also had a similar problem

My problem was solved by doing the following:

enter image description here

enter image description here

BehrouzMoslem
  • 9,053
  • 3
  • 27
  • 34
2

Install Entity Framework in each project (Ex: In Web, In Class Libraries) from NuGet Package Manager orelse Open Tools - Nuget Package Manager - Package Manager Console and use Install-Package EntityFramework to install the Entity Framework.

Don't need to add the below code in every config file. By default it will be added in the project where the database is called through Entity Framework.

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

Chandan Y S
  • 968
  • 11
  • 21
1

This issue could be because of wrong entity framework reference or sometimes the Class name not matching the entity name in database. Make sure the Table name matches with class name.

Ranjan
  • 11
  • 1