No Entity Framework provider found for the ADO.NET

2019-01-01 09:35发布

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

30条回答
有味是清欢
2楼-- · 2019-01-01 10:21

Deleting the BIN-Folder did it for me

查看更多
谁念西风独自凉
3楼-- · 2019-01-01 10:23

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);
    }
    
查看更多
裙下三千臣
4楼-- · 2019-01-01 10:25

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.

查看更多
情到深处是孤独
5楼-- · 2019-01-01 10:25

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/

查看更多
一个人的天荒地老
6楼-- · 2019-01-01 10:26

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

查看更多
与君花间醉酒
7楼-- · 2019-01-01 10:26

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.

查看更多
登录 后发表回答