Do I have to reference the Entity Framework 6 dll in all Projects? (not only my DAL)

送分小仙女□ 提交于 2019-12-22 10:56:51

问题


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?


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



回答2:


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))]


来源:https://stackoverflow.com/questions/20132283/do-i-have-to-reference-the-entity-framework-6-dll-in-all-projects-not-only-my

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!