7
votes

I created a WCF Service using dotNet 4.5. The database layer is built with Entity Framework 6.

I hosted the service with IIS 8. It is working fine.

Now I need to consume the service with windows forms client, that is built using dotnet framework 3.5.

I can create the service reference successfully. But when I run the code, getting the following error;

The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

Any suggestions?

5
Is DataAccessLayer in a different project than your service layer? Did you installed EF 6 Package on WCF project? - It doesn't seem to be related to .net 3.5 or client, it seems to be related to server.Reza Aghaei
Yes there are multiple projects. First there is WCF library, and that has also entity framework 6 installed. Then there is a WCF host application, that hosts the service in IIS. Then there is a windows forms project in dotNet 3.5. In the windows forms project there is no entity framework installed.Ali Sakhi

5 Answers

23
votes

You don't need to Install EF6 NuGet Package in client application, The error is not related to client application because the client application connects to WCF Services and don't know even if a database or data access layer exists.

This error is related to lack of EntityFramework.SqlServer.dll in bin folder of your service library and/or host project.

Option 1

The most simple way is to install EF6 using NuGet Package Manager in your WCF Host project too, and if it has been installed before, try uninstalling and installing it again. This way EntityFramework.SqlServer.dll to output directories.

Please note that this way is somehow against n-layered rules because this your libraries above data access layer are dependent to EF6 which is not so good.

Option 2

As a workaround to copy EntityFramework.SqlServer.dll in output directories, make sure Copy Local of this dll is set to true and then put this code in your DbContext constructor:

var ensureDllIsCopied = System.Data.Entity.SqlServer.SqlProviderServices.Instance; 

It ensures EntityFramework.SqlServer.dll is copied to output directory of consumers of data access project. This is a workaround.

Using this way you don't have any dependency to EF in your layers above data access layer.

4
votes

I fixed this by installing entity framework in the WCF host site.

That worked.

3
votes

There's an issue which a number of people have reported when using EF 6 with Code First around the EntityFramework.SqlServer provider (or another provider they are using) not being copied locally. This leads to the following error:

System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

This only occurs when the EntityFramework NuGet package is not installed in the start-up project (i.e. it is only installed in a class library etc.)

This issue isn't specific to Entity Framework, but is a side effect of how MSBuild works out which assemblies need to be deployed. The issue occurs because there is no code in the application that explicitly references types in EntityFramework.SqlServer.dll (or relevant provider assembly) since EF will load the assembly at runtime based on info in the App/Web.config file. As a result, MSBuild does not detect that the assembly is needed and will not copy it to the output directory

0
votes

Many times when you get an error that a dll can't be loaded it is due to a dependency problem.

If you are trying to consume or execute any part of EF in your client then this maybe a problem. If your client is only consuming POCO's then it won't matter what version of .NET your client is targeted to.

The EF 6.1.3 will install in your .NET 3.5 project but it will not run. The only version of EF that supports .NET 3.5 is v1. EF 6 and above will only support .NET 4 and 4.5.

There are many features that require .NET 4 and above (e.g. async)

You can review the version history of EF to see what each version was targeted for at the link below:

https://msdn.microsoft.com/en-us/data/jj574253

0
votes

Also you need to have this in the app.config or web.config of your startup 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>
  <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>
  <connectionStrings>
    <add name="MyDbContext" connectionString="data source=localhost;initial catalog=MyDatabase;persist security info=True;user id=sa;password=mypassword;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
  </connectionStrings>