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.

asked Feb 8, 2014 at 3:14
5
  • so did you consider downgrading or you are trying to get that version to work Commented Feb 8, 2014 at 8:26
  • EF6 has some features that are very useful for my project. Commented Feb 8, 2014 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 Commented Feb 8, 2014 at 15:08
  • Visual Studio 2013 Premium. Commented Feb 8, 2014 at 16:10
  • 2
    Possible duplicate of No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient' Commented Mar 1, 2017 at 22:31

13 Answers 13

52

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.

answered Apr 27, 2014 at 22:35
4
  • 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! Commented Mar 20, 2015 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 :) Commented Oct 31, 2017 at 12:16
  • 2
    Jan 2019 and this is still a beautiful fix. Thanks for posting this. Commented Jan 14, 2019 at 23:10
  • Thank you for this. I realize I'm late to the show, but this resolved my issue. Commented Jul 8, 2019 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>
answered Feb 9, 2014 at 2:12
5
  • 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 Commented Feb 9, 2014 at 13:54
  • I have same issue but NO SOLUTION :( Commented Mar 9, 2015 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... Commented Nov 14, 2016 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. Commented Dec 14, 2017 at 7:13
  • This fixed my problem as well, but not sure it's an correct approach. Thanks anyway for the fix :) Commented May 27, 2018 at 2: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.

answered Dec 16, 2014 at 6:50
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,8449 gold badges70 silver badges120 bronze badges
answered Jul 9, 2014 at 10:21
2
  • Right-click the Solution Root & manage the Nuget packages. easy.WIN. I added a project and it was missing from the new project, naturally. Commented Apr 17, 2015 at 12:11
  • I got your point and its also correct. Improve my answer in case you wish. Thanks :) Commented Apr 17, 2015 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".

answered May 5, 2015 at 19:10
0
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.

answered Feb 24, 2016 at 12:51
7
  • Wait what, the code you provided does nothing to fix the error from the OP? You're not even using 'instance'. Commented Feb 25, 2016 at 23:36
  • 1
    Leon, I am using instance! Commented Mar 1, 2016 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 Commented Apr 12, 2016 at 8: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. Commented Apr 22, 2016 at 13:57
  • 1
    Thank you sir, that was missing in my code also. It seems that it cannot get the instance from the App.config file :) :) Commented Aug 18, 2017 at 9:51
3

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

answered Aug 15, 2018 at 14:18
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>
answered Feb 8, 2014 at 17:17
1
  • 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. Commented Feb 9, 2014 at 2: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.

answered Apr 17, 2014 at 13:55
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
8,07111 gold badges53 silver badges79 bronze badges
answered May 20, 2015 at 15:45
2

I also had a similar problem

My problem was solved by doing the following:

enter image description here

enter image description here

answered Oct 12, 2017 at 15:14
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>

answered Nov 2, 2018 at 6:36
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.

answered Sep 29, 2015 at 12:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.