EntityFramework.SqlServer.dll not is getting added to the published folder only when I publish in RELEASE mode

懵懂的女人 提交于 2019-12-03 12:25:02
Ahmed Magdy

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?

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

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