4

Question
- Do applications really have to install EF6 to consume another class library that used EF6 as it's underlying data retrieval mechanism (or am I mistaken)?
- How can I work around this and still use EF?

Scenario
We are rewriting an old DAL with a new version that uses EF6 to get it's data. Consumer apps don't call on the EF context. They instead call intermediate functions (in a Business Logic folder in the DAL project), that in turn calls on EF.

When I have a consuming app reference my new DAL (connection string and provider references added to it's .config), the compiler complains of a missing provider:

No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'.

I can remedy this by installing the EF6 package into my consuming application, but this is problematic. We have tons of consuming apps, many with parallel data access mechanisms that often include older versions of EF. I need my DAL to be independent from my consumers.

Can this be done?

Zanon
  • 29,231
  • 20
  • 113
  • 126
maulkye
  • 427
  • 5
  • 13
  • No response in 17 hours makes me nervous that I've asked the wrong question. I know now that I can add the EF dlls to my consuming app and it works with no fuss, but why doesn't the application draw on the providers supplied in the Class Library (DAL) instead of demanding they be in each and every consuming application? Especially when the consuming app provides none. – maulkye Nov 22 '13 at 15:09
  • I backed out to EF5 and this problem did not occur. Not really an answer to my original question though. – maulkye Nov 22 '13 at 18:48
  • I have exact same problem. little difference is it is compiling succesfully. But on runtime as soon as a method in BIZ layer is invoked, it throws IO.FileLoadException. OCuldn´t load file or assembly EntityFramework v 6.0.0.0. I was carefull to not use the domain context objects in the client, only native types but still it is somehow asking for EF dll. Any hints so far. did you try the responses below? – Ricker Silva Oct 29 '18 at 15:18
  • Related post - [Entity Framework Provider type could not be loaded?](https://stackoverflow.com/q/14033193/465053) – RBT Jul 30 '21 at 07:38

2 Answers2

1

I had the same issue when migrating from EF4 to EF6. Adding the EntityFramework.SqlServer.dll as a reference to your DAL library solves the problem for the DAL connection but not for the consuming apps.

The issue occurs because this dll is used only through reflection and as it is not necessary in compile time, it is not published in consuming apps. The hack solution is to make an useless reference just to force the dll to be copied.

Like that:

public class MyAppContext : DbContext
{
    public MyAppContext()
    {
        // hack to force the EntityFramework.SqlServer.dll to be copied when another project references this one
        var forceDllCopy = System.Data.Entity.SqlServer.SqlProviderServices;
    }
}
Zanon
  • 29,231
  • 20
  • 113
  • 126
  • I have that dll referenced in my DAL, where de edmx model is. Above that I have a BIZ layer with business logic using the DAL. BIZ layer also references EF. Above that is a web site which also has a reference to EF 4, for some legacy stuff Ican not touch right now, and it is throwing exception for not finding EF v6.0.0.0 assembly. – Ricker Silva Oct 29 '18 at 15:21
1

Add following class to your data access project which referencing EntityFramework

[AttributeUsage(AttributeTargets.Assembly, AllowMultiple = true)]
public class ReferenceTypeAttribute : Attribute
{
    private readonly Type _type;

    public ReferenceTypeAttribute(Type type)
    {
        _type = type;
    }
}

then add to AssemblyInfo.cs of this project next line

[assembly: ReferenceType(typeof(System.Data.Entity.SqlServer.SqlProviderServices))]
Pavel
  • 524
  • 4
  • 8
  • I tried this but, unless it compiles succefully, when the BIZ method is invoked from the client, it throws exception for not finding the EF6 dll – Ricker Silva Oct 29 '18 at 15:28