36

We are using EntityFramework 6 with Code First. We have a console app that has no reference to EntityFramework but reads the connection string from its App.config. It calls the DatabaseInitializationUtilities assembly passing the connection string as a parameter.

DatabaseInitializationUtilities has the reference to EF6 (EntityFramework and EntityFramework.SqlServer). Its App.config is this:

<?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>
     <system.serviceModel>
         <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IAuthentication" />
            </basicHttpBinding>
         </bindings>
         <client>
            <endpoint address="http://localhost/SecurityServices/Authentication.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAuthentication" contract="SecurityService.IAuthentication" name="BasicHttpBinding_IAuthentication" />
         </client>
      </system.serviceModel>
      <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>
   </configuration>

When execution reaches a line where DatabaseInitializationUtilities attempts to run a script

context.Database.ExecuteSqlCommand(script.ScriptText)

the error is thrown:

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 believe the remedy is exactly what I have in my config file, so I don't understand the problem.

NOTE: Resharper is bluelining the node and reporting "The element 'EntityFramework' has an invalid child element 'providers'. However, the section was injected by NuGet when I installed EF6.

Any ideas?

Dewey
  • 904
  • 1
  • 10
  • 21
  • Take a look at the following question: http://stackoverflow.com/questions/14033193/entity-framework-provider-type-could-not-be-loaded – Lloyd Jan 17 '14 at 00:35
  • Thank you. The comment to [http://stackoverflow.com/questions/14033193/entity-framework-provider-type-could-not-be-loaded][1] was helpful. [1]: http://stackoverflow.com/questions/14033193/entity-framework-provider-type-could-not-be-loaded – Dewey Jan 17 '14 at 16:33
  • 1
    Add EntityFramework.SqlServer.dll to the client app and add connectionstrings to its app.config file did the trick. Now the error is gone http://stackoverflow.com/questions/18455747/no-entity-framework-provider-found-for-the-ado-net-provider-with-invariant-name – Junior Mayhé Aug 15 '14 at 23:20
  • In our case, the EF assemblies were GACed. In this case, you should use the full assembly name in the config file: – pholpar Nov 16 '16 at 11:24
  • 2
    Possible duplicate of [No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'](http://stackoverflow.com/questions/18455747/no-entity-framework-provider-found-for-the-ado-net-provider-with-invariant-name) – Rosberg Linhares Mar 01 '17 at 22:24

10 Answers10

53

You need to create a reference, so it will be copied in the debug folder. So later it can accessed in runtime.

Don't to copy any files, just create this reference:

private volatile Type _dependency;

public MyClass()
{
    _dependency = typeof(System.Data.Entity.SqlServer.SqlProviderServices);
}
hdev
  • 6,097
  • 1
  • 45
  • 62
  • 6
    Epic win! The reason it fails is that only assemblies that are used directly get copied. This assembly has no direct reference so does not get copied. – satnhak Apr 03 '14 at 10:49
  • 11
    I think the EF team have some big explaining up to do on this one. – ProfK Oct 30 '14 at 07:42
  • Download the dll and add it manually to your primary app. Make sure 'Copy Local = true' under properties. I would not recommend the solution to the question as the workaround code will be deleted when you update your entity model. – DJJ Dec 06 '16 at 09:06
  • That is really interesting. Not sure how that works, but it does work :-) – AH. Nov 12 '17 at 12:48
17

do you have the assembly EntityFramework.SqlServer.dll in your application path? I had the same Problem and after copying the dll into the application path every Thing worked fine.

Thomas
  • 171
  • 2
  • i ran into the same problem and fixed by adding the EntityFramework.SqlServer.dll which was missing – Anil C May 23 '14 at 12:12
13

Finally found the answer in this blog:

http://robsneuron.blogspot.com/2013/11/entity-framework-upgrade-to-6.html

Motty
  • 579
  • 1
  • 6
  • 9
5

I had this problem too. Everything worked locally when I ran my .net mvc app but when I published it I got this error. The problem was that I had to reference the EntityFrameworl.SqlServer also in my web project alhough I had seperate project for Data. What is strange is that this error was only thrown when app was published. This is some serious bug probably from Microsoft. I used EF 6.1.1.

TheMentor
  • 464
  • 9
  • 25
  • I had this exact same problem. My reference to the ef assemblies is in a seperate data project referenced from my mvc project. I installed ef from nuget to my mvc project, re-published and it worked. Thanks for the fix – Mike Mar 26 '16 at 17:10
4

This is because EntityFramework.SqlServer.dllis not copied into your project. Add that dll and it will hopefully work.You can find that from the project where you added datamodel.

Baqer Naqvi
  • 6,011
  • 3
  • 50
  • 68
2

For me it turned out that my provider reference in the web.config file was incorrect. I believe that the nuget package might not properly include the provider.

I replaced my provider with this and it worked:

  <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>
David Price
  • 170
  • 1
  • 16
2

I also had a similar problem

My problem was solved by doing the following: enter image description here

enter image description here

BehrouzMoslem
  • 9,053
  • 3
  • 27
  • 34
0

The best way to get ride of such a kind of problem is to include the reference EntityFramework.SqlServer.dll into your project the press F4 (op the properties) change the property to Copy to Local =True so each and every time when publish the app it will be copied to the published folder.

0

I tend to add EntityFramework to the web project as well as the assembly that actually references it.

David Hyde
  • 902
  • 9
  • 18
0

Add "EntityFramework.SqlServer.dll" into your project bin folder will resolved your issue.

vishal
  • 233
  • 4
  • 7