When using Entity Framework Core in an ASP.Net Core application on Visual Studio 2017 I intermittently get the error "Unable to load 'sni.dll'.
Strangely though I have found a temporary fix: restarting my PC.
I don't know why the error keeps happening, I don't know what the variable could be. Could anyone shed light on the possible cause? I would offer a full stack trace but it hasn't happened since deciding to post on here, when it next happens I'll be sure to update this. I know for certain though (as the debug catches the exception) that it happens when EF has cause to get data from the db and transform to entities.
Thanks in advance for any suggestions.
--EDIT This is still happening. Managed to remember to capture the stack trace:
System.DllNotFoundException occurred HResult=0x80131524 Message=Unable to load DLL 'sni.dll': The specified network name is no longer available. (Exception from HRESULT: 0x80070040) Source= StackTrace: at System.Data.SqlClient.SNINativeMethodWrapper.UnmanagedIsTokenRestricted(IntPtr token, Boolean& isRestricted) at System.Data.Win32NativeMethods.IsTokenRestrictedWrapper(IntPtr token) at System.Data.ProviderBase.DbConnectionPoolIdentity.GetCurrent() at System.Data.ProviderBase.DbConnectionPoolGroup.GetConnectionPool(DbConnectionFactory connectionFactory) at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPool(DbConnection owningObject, DbConnectionPoolGroup connectionPoolGroup) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.Open() at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open() at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable.Enumerator.BufferlessMoveNext(Boolean buffer) at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](Func
2 operation, Func2 verifySucceeded, TState state) at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_ShapedQuery>d__3
1.MoveNext() at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_Include>d__301.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable
1 source) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass20_01.<CompileQueryCore>b__0(QueryContext qc) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable
1 source)
Adding nuget package System.Data.SqlClient v4.4.0 solved the same problem for me. The problem is not specific to Entity Framework. It is caused by older version of SqlClient dependency on APIs not available in dotnet Core 2.
For Entity Framework Core 3.x, install the Microsoft.Data.SqlClient Nuget package instead of System.Data.SqlClient.
In some instances this issue can be caused by IIS not having sufficient permissions to access areas of your %USERPROFILE%.nuget
This can be solved in one of two simple ways:
Grant permission to the IIS appPool (like DefaultAppPool or FooAppPool) to access not only the .Nuget folder (this is usually done automatically).
Set the AppPool being used to run as 'LocalSystem' under Identity, this way the appPool will be treated as having higher permissions and should have access.
Here's a series of references for where other users have had this error, 'solved' several years ago:
https://github.com/Azure/app-service-announcements-discussions/issues/9
https://github.com/dotnet/cli/issues/5262
https://github.com/dotnet/corefx/issues/5252
https://github.com/dotnet/corefx/issues/30518
All of these reference the version of SQLClient (while this is relevant, it's a legacy issue), but one smart guy @azisoft (this link) realised this and has helped many thanks to it.
I had the same Problem with a .NET 4.8 WebApplication with MVC5 and WebApi that is using EF Core 3.15.
Out code is seperated in several Projects. I only addeded the EF Core References to the Projects that needs to access DB. This leads to the same Problem as you guys have here.
After i added the EF Core References also to the startup Project it works as intended.
Hope this helps someone.
This is occurred due to System.Data.SqlClient.TO resolve that,
1)Right click on your project and click on manage nuget package.
2)install Microsoft.Data.SqlClient instaed of System.Data.SqlClient
3)rebuild project and publish
(I am using .net core 2.0 web api)
If you are using source control where EFCore was newly added when pulling changes, try restore nuget packages and then close and reopen Visual Studio.
Our current guessing that this line in the project file wasn't executing correctly when the package was restored
<Import Project="..\..\packages\Microsoft.Data.SqlClient.SNI.1.1.0\build\net46\Microsoft.Data.SqlClient.SNI.targets" Condition="Exists('..\..\packages\Microsoft.Data.SqlClient.SNI.1.1.0\build\net46\Microsoft.Data.SqlClient.SNI.targets')" />
This seems like a silly answer but it just worked for us.
User contributions licensed under CC BY-SA 3.0