ChatGPT解决这个技术问题 Extra ChatGPT

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

After downloading the EF6 by nuget and try to run my project, it returns the following error:

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.

https://i.stack.imgur.com/78mKj.png

I use EF5 without the providers and provider stuff, so consider removing it?
put a copy of your connection string here
The connection string is in the picture(App.confing), by the way is very simple, I call the constructor, public BaseStorage(): base ("RaptorDB") {}, BaseStorage() inherits from DbContext in EF5 everything worked perfectly, not already in EF6.
Problem will be solved by installing EF6, the second project(Console), thanks to everyone who helped in any way!
For me, this seemed to be caused by Visual Studio not realizing that the EntityFramework.SqlServer assembly was actually used by the base project. If you do something like @Carra's answer, you don't have to add EF to each project that references your base project - much cleaner.

J
Jim Aho

I just got into the same problem and it looks like EntityFramework although installed from NuGet Package Manager was not correctly installed in the project.

I managed to fix it by running the following command on Package Manager Console:

PM> Install-Package EntityFramework

PMC wrote that 'EntityFramework 6.0.1' already installed, but added it to my console app (which accutally is NOT using EF), but it did the trick for me as well. I I remove EF from console app references, error returns, i don't get this - my console app is using repository project (which uses EF) Thanks for help!
Don't forget to add -ProjectName to the command line if you have several projects in your solution...!!!
Using -Pre option tell nuget to install prerelease packages. I not recommend using it. I have a similar error but the solution was to just install EntityFramework in the host project. I have installed it in a class library but not in the main project (web/console/or whatever),
Same problem here. I had a project that did not have a reference to EF but the EF dll was in the Debug folder. Running this command against this project added EntityFramework.SqlServer.dll to the Debug folder - problem solved.
In my situation this error did not present itself until I deployed the project onto our test server. Indeed it was EntityFramework.SqlServer.dll that was missing and installing EF through package manager worked. It just added the two relevant references to the project and then added the entityFramework settings to the web.config. I guess the local IIS was able source the assembly locally but the full IIS on the web server couldn't due to permissions?
C
Clément Picou

You've added EF to a class library project. You also need to add it to the project that references it (your console app, website or whatever).


This is an absolutely ridiculous answer. Why on earth would I need to do that? And you know what is even more ridiculous? It works.
See my answer below, you don't need to install EF in your console application.
You answer is correct. Only add reference EntityFramework.SqlServer.dll to frontend project that use a library with EF, fix the problem. So do not use this EF (only the DLL)
You do not have to add a reference to EF in the console/web app. You just need to ensure EntityFramework.SqlServer.dll is being copied to the bin directory. Adding a strong reference might break your architecture (if you built multiple tiers, your top-level executing assembly shouldn't even know about EF). Instead, you could ensure the SQL Server provider is copied. See for example stackoverflow.com/a/19130718/870604
I suspect the reason that the EntityFramework.SqlServer.dll isn't detected as a dependency is because the Entity Framework loads it dynamically. How is your project supposed to know to copy over the SQL provider when the only reference to it is in the config file?
P
Phil

You don't need to install Entity Framework in your Console application, you just need to add a reference to the assembly EntityFramework.SqlServer.dll. You can copy this assembly from the Class Library project that uses Entity Framework to a LIB folder and add a reference to it.

In summary:

Class Library application: Install Entity Framework Write your data layer code app.config file has all the configuration related to Entity Framework except for the connection string.

Install Entity Framework

Write your data layer code

app.config file has all the configuration related to Entity Framework except for the connection string.

Create a Console, web or desktop application: Add a reference to the first project. Add a reference to EntityFramework.SqlServer.dll. app.config/web.config has the connection string (remember that the name of the configuration entry has to be the same as the name of the DbContext class.

Add a reference to the first project.

Add a reference to EntityFramework.SqlServer.dll.

app.config/web.config has the connection string (remember that the name of the configuration entry has to be the same as the name of the DbContext class.

I hope it helps.


Correct answer. NO need to install EF. EntityFramework.SqlServer.dll .
I have to agree. This is completely the correct answer. Reference a dll that is 1/2 mb or pull the EF nuget project which is >5.5 mb. Reduces a little the worth of architecting multi tiers too. Poor show from MS really: I have 4 tiers and my top tier should really have no reason to know anything about EF
Still ridiculous anyway. e.g. Why does a front end would need a reference to SqlServer? The front end couldn't care less, in my case. But it works. +1
This was helpful. Thanks a lot.
Won't this make it hard to update versions of EntityFramework? You'd have to remember to go and update the reference to the DLL
R
Ravi Ram

You can also see this message if you forget to include "EntityFramework.SqlServer.dll".

It appears to be a newly added file in EF6. Initially I hadn't included it in my merge module and ran into the problem listed here.


I ran into this problem when I previously had a project (a) with a reference to a project (b) which had a reference to EF. After cleaning & deleting project (a) bin folder, then rebuilding, the EF reference came across, but not EF.SqlServer.dll. Copying this in manually worked for me
@dan richardson thanks for mentioning to 'delete bin folder'.
I got the error when trying to run a LINQPad script after an EF6 upgrade. Even referencing EntityFramework.SqlServer.dll in LINQPad did not fix it UNTIL I rebuilt my solution in VS2013. The new reference then resolved properly in LINQPad and my script ran!
In my case I was ok at dev enviroment but when I published appears the referenced issue. After compare the list of libraries in dev against the bin folder in the server I noticed the absent of EntityFramework.SqlServer.dll, I just to upload it and refresh the app, and voila it fixed.
This was the issue for me, thanks! See the clean solution by @Anders to avoid issues forgetting to include the DLL in every required project.
A
Anders

Instead of adding EntityFramework.SqlServer to host project you can ensure a static reference to it from your Model/entity project like this

static MyContext()
{
    var type = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
    if(type == null)
        throw new Exception("Do not remove, ensures static reference to System.Data.Entity.SqlServer");
}

This will make the build process include the assembly with the host project.

More info on my blog http://andersmalmgren.com/2014/08/20/implicit-dependencies-and-copy-local-fails-to-copy/


I think this is a nice clean solution where we don't have to include references to persistence related DLLs in projects that should be persistence agnostic.
Agreed, and it applies to any library with implicit dependencies not just persistence
When you have an explicit dependency on a type in a assembly it will be copied by the build process. However here you don't have an explicit dependency, and the build process will fail to copy the assembly to the build folder. My code just make sure there exists a explicit reference to any type in said assembly.
Without it there is no explicit dependency to the assembly from your code and it will not be copied to output
This is brilliant.
U
Umar Abbas

When you install Entity Framework 6 through Nuget. EntityFramework.SqlServer sometime miss for another executable. Simply add the Nuget package to that project.

Sometimes above does not work for Test Project

To solve this issue in Test Project just place this Method inside Test Project:

public void FixEfProviderServicesProblem()
{
    var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
}

This method is never been called, but as my observations, the compiler will remove all "unnecessary" assemblies and without using the EntityFramework.SqlServer stuff the test fails.


Well this isn't pretty but it fixed the issue I was having in my test project. None of the other solutions worked.
In my case it was enough to add the Entity Framework also to my test project in "Manage Nuget packets for the solution"
actualy you need to put into any project (not only test) to be insured that System.Data.Entity.SqlServer will be included into "results lib set" after compilation (note: Unity or other IoC tool could change this rule and you will need to call this code from test project).
This is actually the best solution, because you don't have to spray entity framework references everywhere in your project.
This pointed me to the right direction. The EntityFramework.SqlServer is added to your class library, but if not used, it will not be placed inside the output folder of your application. I fixed the issue by adding an ExecutionStrategy, which I still needed to do, so adding a line like SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy()); inside a DbConfiguration class fixed the problem.
J
Johannes

Add this function

private void FixEfProviderServicesProblem()

to database context class in the library class and the missing DLL EntityFramework.SqlServer.dll will be copied to the correct places.

namespace a.b.c
{
    using System.Data.Entity;

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

        public virtual DbSet<WorkflowDefinition> WorkflowDefinitions { get; set; }
        public virtual DbSet<WorkflowInstance> WorkflowInstances { get; set; }
        public virtual DbSet<EngineAlert> EngineAlerts { get; set; }
        public virtual DbSet<AsyncWaitItem> AsyncWaitItems { get; set; }
        public virtual DbSet<TaskItem> TaskItems { get; set; }
        public virtual DbSet<TaskItemLink> TaskItemLinks { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
        }

        private void FixEfProviderServicesProblem()
        {
            // The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer'
            // for the 'System.Data.SqlClient' ADO.NET provider could not be loaded. 
            // Make sure the provider assembly is available to the running application. 
            // See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.
            var instance = System.Data.Entity.SqlServer.SqlProviderServices.Instance;
        }
    }
}

.


sorry trying to revoke it ... as i didn't think it would work... and it does!... it says i cant change my vote, unless the answer is edited, cozs its been too long and has since been locked...
This worked for me too. We have a library project that uses EF 6, and a console application that uses the library. We were getting the same exception as the OP. We do not wish to place EntityFramework-specific configuration in the application configuration file, so this method worked for us. Thanks
Where do you call FixEfProviderServicesProblem I tried in the constructor, with no luck.
I never call it - don't have to. The fact that is is there makes .net think its needed and includes the EntityFramwork as dependency.
Probably from stackoverflow.com/a/19130718/1467396 ? But +1, anyway for the clarity in how/where to use it.
C
Community

None of these worked for me. I did find the solution in another stackoverflow question. I'll add it here for easy reference:

You need to make a reference, so it will be copied in den application path. Because later it will be referenced in runtime. So you don't need to copy any files.

private volatile Type _dependency;

public MyClass()
{
    _dependency = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
}

This! No need to add a reference to other projects that might reference this assembly.
L
Leonel B.

The startup project that references the project where Entity Framework is being used needs the following two assemblies in it's bin folder:

EntityFramework.dll

EntityFramework.SqlServer.dll

Adding a <section> to the <configSections> of the .config file on the startup project makes the first assembly available in that bin directory. You can copy this from the .config file of your Entity Framework project:

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

To make the second .dll available in the bin folder, although not practical, a manual copy from the bin folder of the Entity Framework project can be made. A better alternative is to add to the Post-Build Events of the Entity Framework project the following lines, which will automate the process:

cd $(ProjectDir)
xcopy /y bin\Debug\EntityFramework.SqlServer.dll ..\{PATH_TO_THE_PROJECT_THAT_NEEDS_THE_DLL}\bin\Debug\

Thanks, I have Entity framework in a data layer so its isolated but it is unfortunate that microsoft wont allow us to truely isolate the data layer and makes us pollute the ux with a database technology. I was hoping i wouldnt have to do that.
P
Pragmateek

I got the same error while using Entity Framework 6 with SQL Server Compact 4.0. The article on MSDN for Entity Framework Providers for EF6 was helpful. Running the respective provider commands as nuget packages at Package Manager Console might solve the problem, as also NuGet packages will automatically add registrations to the config file. I ran PM> Install-Package EntityFramework.SqlServerCompact to solve the problem.


I'm truly amazed nobody voted for this so far! The error message states clearly: the reason for the error is that after the EF upgrade, there's really no provider definition left for the SQL Compact in the application's web.config file! Adding the package you mention fixes the web.config file, and there will be provider defined.
simply life saver. It should be marked as answer as it clearly provides solution to the problem
K
Kamil Budziewski

When the error happens in tests projects the prettiest solution is to decorate the test class with:

[DeploymentItem("EntityFramework.SqlServer.dll")]

It´s pretty indeed, but generates more work and is easier to forget. With the "forced reference" trick, you only need to do it on the project(s) that really need to use EF.
C
Community

Ran into this problem today when working with a set of web services, each in different projects, and a separate project containing integration tests for some of those services.

I've been using this setup for some time with EF5, without needing to include references to EF from the Integration Test Project.

Now, after upgrading to EF6, it seems I need to include a reference to EF6 in the integration test project too, even though it is not used there (pretty much as pointed out above by user3004275).

Indications you're facing the same problem:

Calls directly to EF (connecting to a DB, getting data, etc) work fine, as long as they are initiated from a project that has references to EF6.

Calls to the service via a published service interface work fine; i.e. there are no missing references "internally" in the service.

Calls directly to public methods in the service project, from a project outside the service, will cause this error, even though EF is not used in that project itself; only internally in the called project

The third point is what threw me off for a while, and I'm still not sure why this is required. Adding a ref to EF6 in my Integration Test project solved it in any case...


T
Taran

Add below to your app.config.

 <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>

You also need to register it to <configSections> - <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
This is what "IInstall-Package EntityFramework" does. This is not really necessary because EntityFramework by default tries to load EntityFramework.SqlServer.dll for the SqlClient invariant name. This method can be used to substitute the provider.
O
Ondřej

I just run into this problem today. I have data repository class library with EF63 NuGet package and console application for testing, which have reference only to class library project. I created very simple post-build command, which copies EntityFramework.SqlServer.dll from class library's Bin\Debug folder to console application's Bin\Debug folder and problem solved. Do not forget to add entityFramework section to console application's .config file.


B
BehrouzMoslem

I also had a similar problem.My problem was solved by doing the following:

https://i.stack.imgur.com/6ibKF.png

https://i.stack.imgur.com/YyRro.png


u
user2956314

You are just missing a reference to EntityFramework.SqlServer.dll. For EntityFramework projects using SQL Server, the two files you need to refer are EntityFramework.SqlServer.dll and EntityFramework.dll


D
David

Deleting the BIN-Folder did it for me


R
Rosberg Linhares

You should force a static reference to the EntityFramework.SqlServer.dll assembly, but instead of putting a dummy code, you can do this in a more beautiful way:

If you already have a DbConfiguration class: public class MyConfiguration : DbConfiguration { public MyConfiguration() { this.SetProviderServices(System.Data.Entity.SqlServer.SqlProviderServices.ProviderInvariantName, System.Data.Entity.SqlServer.SqlProviderServices.Instance); } } If you don't have a DbConfiguration class you must put the following code at app startup (before EF is used): static MyContext() { DbConfiguration.Loaded += (sender, e) => e.ReplaceService((s, k) => System.Data.Entity.SqlServer.SqlProviderServices.Instance); }


y
yaserjalilian

just Copy EntityFramework.SqlServer.dll into bin folder


K
Kuntal Ghosh

I have just re-installed the Entity Framework using Nuget. And follow the instruction written on the link below : http://robsneuron.blogspot.in/2013/11/entity-framework-upgrade-to-6.html

I think the problem will get solved.


An explanation would be nice! Why you have to re-install it and so on
Because for some unknown reason(s) the changes will not going to take effect and so I re-installed the EntityFramework 6.1.1 and after that it takes effect.
u
user2347528

Expand YourModel.edmx file and open YourModel.Context.cs class under YourModel.Context.tt.

I added the following line in the using section and the error was fixed for me.

using SqlProviderServices = System.Data.Entity.SqlServer.SqlProviderServices;

You may have to add this line to the file each time the file is auto generated.


W
Willy David Jr

I have the same error. It's weird that it only happens whenever I used my dbContext to query to any of my model or get its list like:

var results = _dbContext.MyModel.ToList();

We tried to reinstall the Entity Framework, reference it properly but to no avail.

Luckily, we tried to check the Nuget for ALL solutions, then update everything or make sure everything is the same version because we noticed that the two projects has different EF versions on the Web project. And it works. The error is gone.

Here is the screenshot on how to Manage Nuget for all solutions:

https://i.stack.imgur.com/uJYe0.png


V
Vijaya Malla

Just Install EntityFramework package to your Web/Console Project. That should add the section to your config file.


T
Tobi Owolawi

it looks like nobody mentioned first checking if System.Data.SqlClient is installed in the system and if a reference is made to it.

i solved my issue by installing System.Data.SqlClient and adding in a new provider in app.Config

<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6"/>

u
user2588362

Also, make sure you startup project is the project that contains your dbcontext (or relevant app.config). Mine was trying to start up a website project which didnt have all the necessary configuration settings.


S
SharpC

I tried almost all the above and nothing worked.

Only when I set the referenced DLLs in the Default Project EntityFramework and EntityFramework.SqlServer properties Copy Local to True did it start working!


These already exist in the compile time assemblies of EntityFramework (6.4.4). Location is C:\Users\\.nuget\packages\entityframework\6.4.4\lib\netstandard2.1. Can you share a bit more with what you've done to make these "referenced"?
V
Vahid Akbari

everybody I need your Attention that two dll EntityFramework.dll And EntityFramework.SqlServer.dll are DataAccess layer Library And it is not Logical to Use them in view or any other layer.it solves your problem but it is not logical.

logical way is that enitiess attribute remove and replace them with Fluent API.this is real solution


d
dkero

I had one console application and class library. In class library I created Entity Data Model (right click to Class Library > Add > New Item > Data > ADO.NET Entity Data Model 6.0) and put reference inside console application. So, you have console application which has reference to class library and inside class library you have EF model. I had the same error when I tried to get some records from the table.

I resolved this issue by following these steps:

Right click to solution and choose option 'Manage NuGet Packages for Solution' and NuGet package manager window will show up. Go to 'Manage' option under 'Installed packages' TIP: Entity Framework is added to Class Library, so you will have EntityFramework under 'Installed packages' and you'll see 'Manage'option Click on 'Manage' option and check to install package to project which has reference to class library which holds EF model (in my case I set check box to install package to console app which had reference to class library which had EF model inside)

That's all I had to do and everything worked perfect.

I hope it helped.


m
msz

I have the same issue(in my 3-Tire level project) and I fixed it by adding/installing the EF to my main Project.


u
user2662643

I had a related issue when migrating from a CE db over to Sql Server on Azure. Just wasted 4 hrs trying to solve this. Hopefully this may save someone a similar fate. For me, I had a reference to SqlCE in my packages.config file. Removing it solved my entire issue and allowed me to use migrations. Yay Microsoft for another tech with unnecessarily complex setup and config issues.