错误:没有为ADO找到实体框架提供程序。具有不变量名'System.Data.SqlClient'的NET提供程序

时间:2021-11-14 02:13:20

I recently upgraded/updated Entity Framework in an old project from version 4 or 5 to version 6. Now I get this exception:

我最近将旧项目中的实体框架从版本4或版本5升级到版本6。现在我得到了一个例外:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code

类型系统的一个例外。发生在EntityFramework InvalidOperationException”。但是在用户代码中没有处理dll

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.

附加信息:没有为ADO找到实体框架提供程序。具有不变量名'System.Data.SqlClient'的网络提供程序。确保提供程序在应用程序配置文件的“entityFramework”部分注册。参见http://go.microsoft.com/fwlink/?LinkId = 260882的更多信息。

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:

我在谷歌上搜索了错误,发现了几个这样的线程,但是没有一个包含适合我的解决方案。我的App.config是这样的:

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

我已经从我的项目中卸载了实体框架并重新安装了它,删除了所有对旧EF文件的引用并重新安装,但是没有任何东西适合我。我一直得到这个错误。

11 个解决方案

#1


43  

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.

我遇到过这样的问题:我有一个模型项目,它同时引用EntityFramework和. sqlserver程序集,以及一个使用ASP的独立UI项目。净MVC。我想从EF 4.1升级到6.1。实际上,我必须完成这里描述的部分内容:http://robsneuron.blogspot.com/2013/11/entityframeworkupgradeto .html。我想强调的是,我没有在UI项目中添加对这些项目的引用,也没有将配置添加到UI项目的web中。配置,因为这些步骤会违反我的关注点分离。

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.

我所做的是在我的模型项目中,我必须翻转EntityFramework的“复制本地”引用设置。SqlServer(以及EntityFramework引用,为了安全起见)到“False”并保存所有文件,以使项目将 节点放入.csproj文件中,然后将其设置为“True”并再次保存,这样True将作为最终值。

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.

我还必须在它的构造函数中为我的dbcontext派生类添加hack行,以强制使用该程序集,即使该行什么也不做。这两个步骤都是我从博客文章中学到的。

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.

我要感谢我所引用的那个博客的作者,以及所有那些要求和回答问题的人,试图帮助我们克服这个可怕的错误。

#2


33  

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:

好吧,这很奇怪。我有两个项目:一个是UI项目(一个ASP)。NET MVC项目)和其他项目都是类似于存储库的项目。储存库项目有一个对EF的引用,但是UI项目没有(因为它不需要一个,它只需要引用其他项目)。在我为UI项目安装了EF之后,一切都开始工作了。这就是为什么,它将这段代码添加到我的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>

#3


13  

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.

我也有同样的问题,不同的是我没有访问源代码的权限。通过放置entityframe . sqlserver的正确版本,我解决了我的问题。应用程序的bin目录中的dll。

#4


10  

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.

首先=右键单击您的解决方案或项目根,然后单击Manage NuGet包。选择“EntityFramework”,选择适当的项目并单击Ok。

or

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

第二,转到控制台包管理器并运行安装包EntityFramework。

Hope it helps.

希望它可以帮助。

#5


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:

亨德利的答案100%正确。我的应用程序也有同样的问题,其中有一个存储库项目使用封装EF db上下文操作的方法来处理数据库。其他项目使用这个存储库,我不想在这些项目中引用EF。不知为什么我觉得不合适,我只需要在repository project中使用EF。总之,复制EntityFramework.SqlServer。dll到其他项目输出目录解决了这个问题。为了避免问题,当您忘记复制这个dll时,您可以更改repository build目录。转到repository项目的属性,选择Build选项卡,在output部分,可以将output目录设置为其他项目的Build目录。当然,这只是解决方案。也许在某个地方提到的黑客行为更好:

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

我对EF很陌生。有更好的办法来解决这个问题吗?我不喜欢“黑客”——它让我觉得有些东西“不安全”。

#6


5  

I have solved the issue using below code in my DBContext

在我的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.

谢谢你的会员。

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

#8


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.

我也遇到了同样的问题,唯一适合我的是使用uninstall - package从每个项目卸载实体框架包。然后再在整个溶液中。它会要求你选择你想要安装的项目,你应该选择全部。

#9


1  

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

对我起作用的是从EF 6.1.3降级到EF 6.1.1。

In Visual Studios 2012+ head over to:

在Visual studio 2012+转向:

Tools - Nuget Package Manager - Package Manager Console`

工具- Nuget包管理器-包管理控制台'

Then enter:

然后输入:

Install-Package EntityFramework.SqlServerCompact -Version 6.1.1

安装包EntityFramework。SqlServerCompact - version但是

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

我不必首先卸载EF 6.1.3,因为上面的命令已经这样做了。

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

此外,我不知道它是否做了什么,但我也在我的项目中安装了SQL Server CE。

Here's the link to the solution I found:

以下是我找到的解决方案的链接:

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

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

#10


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.

这个问题可能是由于错误的实体框架引用,或者有时是类名不匹配数据库中的实体名称。确保表名与类名匹配。

#11


0  

I also had a similar problem

我也有类似的问题

My problem was solved by doing the following:

我的问题是这样解决的:

错误:没有为ADO找到实体框架提供程序。具有不变量名'System.Data.SqlClient'的NET提供程序

错误:没有为ADO找到实体框架提供程序。具有不变量名'System.Data.SqlClient'的NET提供程序

#1


43  

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.

我遇到过这样的问题:我有一个模型项目,它同时引用EntityFramework和. sqlserver程序集,以及一个使用ASP的独立UI项目。净MVC。我想从EF 4.1升级到6.1。实际上,我必须完成这里描述的部分内容:http://robsneuron.blogspot.com/2013/11/entityframeworkupgradeto .html。我想强调的是,我没有在UI项目中添加对这些项目的引用,也没有将配置添加到UI项目的web中。配置,因为这些步骤会违反我的关注点分离。

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.

我所做的是在我的模型项目中,我必须翻转EntityFramework的“复制本地”引用设置。SqlServer(以及EntityFramework引用,为了安全起见)到“False”并保存所有文件,以使项目将 节点放入.csproj文件中,然后将其设置为“True”并再次保存,这样True将作为最终值。

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.

我还必须在它的构造函数中为我的dbcontext派生类添加hack行,以强制使用该程序集,即使该行什么也不做。这两个步骤都是我从博客文章中学到的。

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.

我要感谢我所引用的那个博客的作者,以及所有那些要求和回答问题的人,试图帮助我们克服这个可怕的错误。

#2


33  

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:

好吧,这很奇怪。我有两个项目:一个是UI项目(一个ASP)。NET MVC项目)和其他项目都是类似于存储库的项目。储存库项目有一个对EF的引用,但是UI项目没有(因为它不需要一个,它只需要引用其他项目)。在我为UI项目安装了EF之后,一切都开始工作了。这就是为什么,它将这段代码添加到我的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>

#3


13  

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.

我也有同样的问题,不同的是我没有访问源代码的权限。通过放置entityframe . sqlserver的正确版本,我解决了我的问题。应用程序的bin目录中的dll。

#4


10  

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.

首先=右键单击您的解决方案或项目根,然后单击Manage NuGet包。选择“EntityFramework”,选择适当的项目并单击Ok。

or

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

第二,转到控制台包管理器并运行安装包EntityFramework。

Hope it helps.

希望它可以帮助。

#5


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:

亨德利的答案100%正确。我的应用程序也有同样的问题,其中有一个存储库项目使用封装EF db上下文操作的方法来处理数据库。其他项目使用这个存储库,我不想在这些项目中引用EF。不知为什么我觉得不合适,我只需要在repository project中使用EF。总之,复制EntityFramework.SqlServer。dll到其他项目输出目录解决了这个问题。为了避免问题,当您忘记复制这个dll时,您可以更改repository build目录。转到repository项目的属性,选择Build选项卡,在output部分,可以将output目录设置为其他项目的Build目录。当然,这只是解决方案。也许在某个地方提到的黑客行为更好:

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

我对EF很陌生。有更好的办法来解决这个问题吗?我不喜欢“黑客”——它让我觉得有些东西“不安全”。

#6


5  

I have solved the issue using below code in my DBContext

在我的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.

谢谢你的会员。

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

#8


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.

我也遇到了同样的问题,唯一适合我的是使用uninstall - package从每个项目卸载实体框架包。然后再在整个溶液中。它会要求你选择你想要安装的项目,你应该选择全部。

#9


1  

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

对我起作用的是从EF 6.1.3降级到EF 6.1.1。

In Visual Studios 2012+ head over to:

在Visual studio 2012+转向:

Tools - Nuget Package Manager - Package Manager Console`

工具- Nuget包管理器-包管理控制台'

Then enter:

然后输入:

Install-Package EntityFramework.SqlServerCompact -Version 6.1.1

安装包EntityFramework。SqlServerCompact - version但是

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

我不必首先卸载EF 6.1.3,因为上面的命令已经这样做了。

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

此外,我不知道它是否做了什么,但我也在我的项目中安装了SQL Server CE。

Here's the link to the solution I found:

以下是我找到的解决方案的链接:

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

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

#10


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.

这个问题可能是由于错误的实体框架引用,或者有时是类名不匹配数据库中的实体名称。确保表名与类名匹配。

#11


0  

I also had a similar problem

我也有类似的问题

My problem was solved by doing the following:

我的问题是这样解决的:

错误:没有为ADO找到实体框架提供程序。具有不变量名'System.Data.SqlClient'的NET提供程序

错误:没有为ADO找到实体框架提供程序。具有不变量名'System.Data.SqlClient'的NET提供程序