11

I know there is a problem with EF6 EntityFramework.SqlServer and included var type = typeof(System.Data.Entity.SqlServer.SqlProviderServices); in context constructor. It works fine when i do publish in DEBUG mode.

Getting the below error only when I publish in RELEASE mode. The reason is EntityFramework.SqlServer.dll missing in the published folder. But, bin folder has EntityFramework.SqlServer.dll for both debug and release mode.

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.

Why is it missing only when I publish using RELEASE mode?

Uba
  • 559
  • 3
  • 6
  • 17
  • 1
    In Solution Explorer, sSelect the reference to EntityFramework.SqlServer.dll and ensure that it's marked as "Copy to Output" in Release configuration and compare it to Debug. – Dai Aug 21 '14 at 18:22
  • Are you sure, you have the same database structure for debug and for release? – libik Aug 21 '14 at 18:25
  • @libik: Yes, both has same structure. Also, verified both debug and release bin folders. It has EntityFramework.SqlServer.dll. – Uba Aug 21 '14 at 18:56
  • @Dai: Do you mean copy local? Because I don't "Copy to Output" property in my EntityFramework.SqlServer.dll properties. – Uba Aug 21 '14 at 18:59
  • Add attribute to your assembly to reference EntityFramework.SqlServer. For details see https://stackoverflow.com/a/48318806/1881344 – Pavel Jan 28 '18 at 08:19

3 Answers3

27

Don't know why; but adding this method to your context will make your project copy the dll

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

Tested and working Reference: Entity Framework Provider type could not be loaded?

Community
  • 1
  • 1
Ahmed Magdy
  • 5,256
  • 8
  • 36
  • 74
1

I also had the same problem.

What happens is that this EntityFramework.SqlServer.dll is not referenced directly by your project, but internally byEntityFramework.dll.

Since we do not use any of this EntityFramework.SqlServer.dll in our code, the Roslyn compiler, applying some optimizations, understands that it does not need this component and then does not copy it to thebin folder and does not copy also for Publish.

What you need to do is create a reference to some object of this EntityFramework.SqlServer.dll anywhere in your code.

For example, I have a dependency injection container configuration class, where I configure my repositories and contexts, and I think it would be a good location. So I created a static property that I never use in any location, but it solves the problem:

using System.Data.Entity.SqlServer;

public class ContainerConfiguration
{
    // HACK: This code snippet ensures that the DLL (EntityFramework.SqlServer.dll)
    // not removed by compiler optimizer (csc.exe roslyn)
    public static SqlProviderServices EntitySqlServerHack => SqlProviderServices.Instance;

    public void ContainerConfiguration(IContainer container)
    {
        InternalConfigure(container, false);
    }
}
Erlimar
  • 11
  • 3
-1

Sounds like something is corrupted in your solution. I would remake the solution and copy all the files to the new one.

Clayton Harbich
  • 492
  • 1
  • 7
  • 14