Should I reference Entity Framework 6 DLL in all projects? (not just my dal)

Question
- Do applications really need to install EF6 to use another class library that used EF6 as a data retrieval mechanism (or am I mistaken)?
- How can I get around this and use EF?

Scenario
We are rewriting the old DAL with a new version that uses EF6 to receive data. Consumer applications do not require an EF context. Instead, they call intermediate functions (in the Business Logic folder in the DAL project), which in turn calls EF.

When I have a link to the application, my new DAL (connection string and provider links are added to it .config), the compiler complains about the missing provider:

The ADTI.NET provider could not be found with the Entity Framework provider with the invariant name "System.Data.SqlClient".

I can fix this by installing the EF6 package in my consumer application, but this is problematic. We have many consuming applications, many of which have parallel data access mechanisms, which often include older versions of EF. I need my DAL to be independent of my customers.

Can this be done?

+1
source share
2 answers

I had the same problem when switching from EF4 to EF6. Adding EntityFramework.SqlServer.dll as a reference to your DAL library solves the problem for connecting DAL, but not for user applications.

The problem arises because this DLL is used only through reflection and, since it is not necessary at compile time, it is not published in consuming applications. The hack solution is to make a useless link only to force dll copying.

Like:

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

Add the following class to a data access project that references EntityFramework

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

then add the following line to AssemblyInfo.cs of this project

 [assembly: ReferenceType(typeof(System.Data.Entity.SqlServer.SqlProviderServices))] 
0
source

Source: https://habr.com/ru/post/974179/


All Articles