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

北战南征 提交于 2019-12-05 23:37:11

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

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