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

后端 未结 3 2039
说谎
说谎 2021-01-02 03:19

I know there is a problem with EF6 EntityFramework.SqlServer and included var type = typeof(System.Data.Entity.SqlServer.SqlProviderServices); in context const

3条回答
  •  盖世英雄少女心
    2021-01-02 03:41

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

提交回复
热议问题