[entity-framework] 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.

enter image description here

This question is related to entity-framework

The answer is


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:

  1. 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);
        }
    }
    
  2. 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<DbProviderServices>((s, k) => System.Data.Entity.SqlServer.SqlProviderServices.Instance);
    }
    

Note: I had this problem while Generating Database Sql from Model. It had created all the tables fine but wouldn't export the changes. What you need to notice is that this error is generated when you try to export the sql using the DDL Generation Template as SSDLtoSQL10. It is expecting MySQL connection here so make sure you select from the drop down DDL Generation Template SSDLtoMySQL on the Model properties. Spent a whole day on this !


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 have the same issue(in my 3-Tire level project) and I fixed it by adding/installing the EF to my main Project.


I had the identical exception thrown. I included

using System.Data; 
using System.Data.Entity;

and everything is back to working again ..


In my case, everything was working properly then suddenly stopped worked because I think Resharper altered some changes which caused the problem. My project was divided into the data layer, service and presentation layer. I had Entity framework installed and referenced in my data layer but still the error didn't go away. Uninstalling and reinstalling didn't work either. Finally, I solved it by making the data layer the Startup project, making migration, updating the database and changing the Startup project back to my presentation layer.


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


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

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

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.


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!


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>

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:

enter image description here


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

enter image description here

enter image description here


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.


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

.


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

I had the same issue, just copied the App Config file from the project that contained the DBContext to my test project


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.


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


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


just Copy EntityFramework.SqlServer.dll into bin folder


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

I hope it helps.


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);
}

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


As message shows that we need to add provider System.Data.SqlClient that's why we need to install nuget package of EntityFramework that has two dll but if we are developing only console application then we just need to add reference of EntityFramework.SqlServer.dll


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


Deleting the BIN-Folder did it for me


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\

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.


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:

  1. Right click to solution and choose option 'Manage NuGet Packages for Solution' and NuGet package manager window will show up.
  2. 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
  3. 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.


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.


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.


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