Sqlclient: Entity Framework Core 3.1.0 System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception. ---> System.ComponentModel.Win32Exception

Created on 25 Dec 2019  路  4Comments  路  Source: dotnet/SqlClient

I am using VS2019 community edition. I am using the .NET Framework 4.7.2. I am having Samurai.domain .NETFramework class library project with 3 domain classes and Samurai.Data project with SamuraiContext. I am using Entity Framework Core 3.1.0 and SQLServer provider. A .NETFramework 4.7.2 Console application. Build was successful. Was able to create a migration in Samurai.Data project.
But I am getting error when I try to update the database. Please help.

PM> update-database
Build started...
Build succeeded.
System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception. ---> System.ComponentModel.Win32Exception: Failed to load E:\Projects\EFCore2\SamuraiApp\SomeUI\bin\Debug\x64\SNI.dll
at Microsoft.Data.SqlClient.SNINativeMethodWrapper..cctor() in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\Interop\SNINativeMethodWrapper.cs:line 66
--- End of inner exception stack trace ---
at Microsoft.Data.SqlClient.SNINativeMethodWrapper.UnmanagedIsTokenRestricted(IntPtr token, Boolean& isRestricted)
at Microsoft.Data.Win32NativeMethods.IsTokenRestrictedWrapper(IntPtr token) in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\Interop\SNINativeMethodWrapper.cs:line 888
at Microsoft.Data.ProviderBase.DbConnectionPoolIdentity.GetCurrent() in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPoolIdentity.cs:line 107
at Microsoft.Data.ProviderBase.DbConnectionPoolGroup.GetConnectionPool(DbConnectionFactory connectionFactory) in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPoolGroup.cs:line 173
at Microsoft.Data.ProviderBase.DbConnectionFactory.GetConnectionPool(DbConnection owningObject, DbConnectionPoolGroup connectionPoolGroup) in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionFactory.cs:line 364
at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) in E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionFactory.cs:line 205 at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionInternal.cs:line 683
at Microsoft.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) in E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1618 at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1599
at Microsoft.Data.SqlClient.SqlConnection.Open() in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:line 1250
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.<>c__DisplayClass18_0.b__0(DateTime giveUp)
at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.<>c__DisplayClass12_02.<Execute>b__0(DbContext c, TState s) at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func3 operation, Func3 verifySucceeded) at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[TState,TResult](IExecutionStrategy strategy, TState state, Func2 operation, Func`2 verifySucceeded)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists(Boolean retryOnNotExists)
at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception.

Most helpful comment

Thank you @cheenamalhotra. This futher explanation if for those people who might face the same issue in the future. I got this issue when I was using creating projects targetting .NET Framework 4.7.2 projects and try to use Entity Framework Core 3.1.0. (
As I said, I had 3 projects under the solution SamuraiApp.

  1. SamuraiApp.Data ( has SamuraiContext)
  2. SamuraiApp.Domain (has the domain classes like Samurai, Battle, Quote etc., )
  3. Console Application.

SamuraiApp.Data was already having Microsoft.Data.SqlClient 1.0.9 and updated to Microsoft.Data.SqlClient 1.1.0 driver. The issue still remained.

After installing Microsoft.Data.SqlClient 1.1.0 on the Console Application, the issue got resolved and the "update-database" command succeeded.

Once again Thank you @cheenamalhotra.

All 4 comments

Hi @dmuruges

Please add additional reference for Microsoft.Data.SqlClient 1.1.0 driver in your application and try again.

Also ensure you have C++ Runtime installed and Nuget.exe version is > 5.0. There are similar issues in this repo where you can find relevant information.

Thank you @cheenamalhotra. This futher explanation if for those people who might face the same issue in the future. I got this issue when I was using creating projects targetting .NET Framework 4.7.2 projects and try to use Entity Framework Core 3.1.0. (
As I said, I had 3 projects under the solution SamuraiApp.

  1. SamuraiApp.Data ( has SamuraiContext)
  2. SamuraiApp.Domain (has the domain classes like Samurai, Battle, Quote etc., )
  3. Console Application.

SamuraiApp.Data was already having Microsoft.Data.SqlClient 1.0.9 and updated to Microsoft.Data.SqlClient 1.1.0 driver. The issue still remained.

After installing Microsoft.Data.SqlClient 1.1.0 on the Console Application, the issue got resolved and the "update-database" command succeeded.

Once again Thank you @cheenamalhotra.

Also ensure you have C++ Runtime installed.

But which runtime on Win2008R2 ? Thanks

@ZeBobo5

With Microsoft.Data.SqlClient.SNI v1.1.1 we have removed dependency on VC Runtime, so you don't need that installed on your client if you reference this package with Microsoft.Data.SqlClient v1.1.1.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

genifycom picture genifycom  路  8Comments

stylesm picture stylesm  路  6Comments

ph1294 picture ph1294  路  8Comments

amitagrawalhyd picture amitagrawalhyd  路  4Comments

jeroen-mostert picture jeroen-mostert  路  7Comments