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

I know that there is a problem with EF6 EntityFramework.SqlServer and var type = typeof(System.Data.Entity.SqlServer.SqlProviderServices); included var type = typeof(System.Data.Entity.SqlServer.SqlProviderServices); into the context constructor. It works great when I publish in DEBUG mode.

Getting the error below only when publishing in RELEASE mode. The reason in EntityFramework.SqlServer.dll missing in the published folder. But the bin folder has EntityFramework.SqlServer.dll for debug and release mode.

Error:

The Entity Framework provider type "System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer 'registered in the application configuration file for the ADO.NET provider with the invariant name" System.Data.SqlClient "failed to load. Make sure the name and that assembly is available for the running application.

Why is it missing only when publishing in RELEASE mode?

+9
source share
3 answers

I do not know why; but adding this method to your context will make your project copy 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 Link: Failed to load Entity Framework provider type?

+22
source

Something seems to be spoiled in your decision. I would redo the solution and copy all the files to a new one.

0
source

I had the same problem too.

What happens is that EntityFramework.SqlServer.dll does not refer directly to your project, but inside EntityFramework.dll .

Since none of these EntityFramework.SqlServer.dll are used in our code, the Roslyn compiler, using some optimizations, understands that it does not need this component and then does not copy it to the bin folder and also does not copy it for Publish .

What you need to do is create a link 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 that would be a good place. Therefore, I created a static property that I never use in any place, 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); } } 
0
source

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


All Articles