dotnet / SqlClient

Microsoft.Data.SqlClient provides database connectivity to SQL Server for .NET applications.
MIT License
842 stars 281 forks source link

Couldn't use Microsoft.EntityFrameworkCore.SqlServer > 6.0.14 #1944

Closed sake402 closed 1 year ago

sake402 commented 1 year ago

Given the connection string in appsettings.json

"ConnectionStrings": {
    "DefaultConnection": "Data Source=.;Initial Catalog=TestDb;MultipleActiveResultSets=True;Integrated Security=True;User Instance=True"
  },

and then

dotnet ef database update

It works with version 6.0.14, however because we need TPC mapping we need to use higher version, but with version 7.0.0 - 7.0.3 there is an error

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is 
corrupt.

Repeat 2 times:
--------------------------------
   at Microsoft.Data.SqlClient.SNINativeMethodWrapper.SNIOpenSyncExWrapper(SNI_CLIENT_CONSUMER_INFO ByRef, IntPtr ByRef)
--------------------------------
   at Microsoft.Data.SqlClient.SNINativeMethodWrapper.SNIOpenSyncEx(ConsumerInfo, System.String, IntPtr ByRef, Byte[], Byte[], Boolean, Boolean, Int32, 
Boolean, Microsoft.Data.SqlClient.SqlConnectionIPAddressPreference, Microsoft.Data.SqlClient.SQLDNSInfo, System.String)
   at Microsoft.Data.SqlClient.SNIHandle..ctor(ConsumerInfo, System.String, Byte[], Boolean, Int32, Byte[] ByRef, Boolean, Boolean, Boolean, 
Microsoft.Data.SqlClient.SqlConnectionIPAddressPreference, Microsoft.Data.SqlClient.SQLDNSInfo, Boolean, System.String)
   at Microsoft.Data.SqlClient.TdsParserStateObjectNative.CreatePhysicalSNIHandle(System.String, Boolean, Int64, Byte[] ByRef, Byte[][] ByRef, Boolean, 
Boolean, Boolean, Microsoft.Data.SqlClient.SqlConnectionIPAddressPreference, System.String, Microsoft.Data.SqlClient.SQLDNSInfo ByRef, System.String, 
Boolean, Boolean, System.String)
   at Microsoft.Data.SqlClient.TdsParser.Connect(Microsoft.Data.SqlClient.ServerInfo, Microsoft.Data.SqlClient.SqlInternalConnectionTds, Boolean, Int64, 
Microsoft.Data.SqlClient.SqlConnectionString, Boolean)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(Microsoft.Data.SqlClient.ServerInfo, System.String, 
System.Security.SecureString, Boolean, Microsoft.Data.ProviderBase.TimeoutTimer, Boolean)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(Microsoft.Data.SqlClient.ServerInfo, System.String, 
System.Security.SecureString, Boolean, Microsoft.Data.SqlClient.SqlConnectionString, Microsoft.Data.SqlClient.SqlCredential, 
Microsoft.Data.ProviderBase.TimeoutTimer)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(Microsoft.Data.ProviderBase.TimeoutTimer, 
Microsoft.Data.SqlClient.SqlConnectionString, Microsoft.Data.SqlClient.SqlCredential, System.String, System.Security.SecureString, Boolean)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(Microsoft.Data.ProviderBase.DbConnectionPoolIdentity, 
Microsoft.Data.SqlClient.SqlConnectionString, Microsoft.Data.SqlClient.SqlCredential, System.Object, System.String, System.Security.SecureString, 
Boolean, Microsoft.Data.SqlClient.SqlConnectionString, Microsoft.Data.SqlClient.SessionData, Boolean, System.String, 
Microsoft.Data.ProviderBase.DbConnectionPool)
   at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(Microsoft.Data.Common.DbConnectionOptions, 
Microsoft.Data.Common.DbConnectionPoolKey, System.Object, Microsoft.Data.ProviderBase.DbConnectionPool, System.Data.Common.DbConnection, 
Microsoft.Data.Common.DbConnectionOptions)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(Microsoft.Data.ProviderBase.DbConnectionPool, 
System.Data.Common.DbConnection, Microsoft.Data.Common.DbConnectionOptions, Microsoft.Data.Common.DbConnectionPoolKey, 
Microsoft.Data.Common.DbConnectionOptions)
   at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(System.Data.Common.DbConnection, Microsoft.Data.Common.DbConnectionOptions, 
Microsoft.Data.ProviderBase.DbConnectionInternal)
   at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(System.Data.Common.DbConnection, Microsoft.Data.Common.DbConnectionOptions, 
Microsoft.Data.ProviderBase.DbConnectionInternal)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(System.Data.Common.DbConnection, UInt32, Boolean, Boolean, 
Microsoft.Data.Common.DbConnectionOptions, Microsoft.Data.ProviderBase.DbConnectionInternal ByRef)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(System.Data.Common.DbConnection, 
System.Threading.Tasks.TaskCompletionSource`1<Microsoft.Data.ProviderBase.DbConnectionInternal>, Microsoft.Data.Common.DbConnectionOptions, 
Microsoft.Data.ProviderBase.DbConnectionInternal ByRef)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(System.Data.Common.DbConnection, 
System.Threading.Tasks.TaskCompletionSource`1<Microsoft.Data.ProviderBase.DbConnectionInternal>, Microsoft.Data.Common.DbConnectionOptions, 
Microsoft.Data.ProviderBase.DbConnectionInternal, Microsoft.Data.ProviderBase.DbConnectionInternal ByRef)
   at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(System.Data.Common.DbConnection, 
Microsoft.Data.ProviderBase.DbConnectionFactory, System.Threading.Tasks.TaskCompletionSource`1<Microsoft.Data.ProviderBase.DbConnectionInternal>, 
Microsoft.Data.Common.DbConnectionOptions)
   at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(System.Data.Common.DbConnection, Microsoft.Data.ProviderBase.DbConnectionFactory, 
System.Threading.Tasks.TaskCompletionSource`1<Microsoft.Data.ProviderBase.DbConnectionInternal>, Microsoft.Data.Common.DbConnectionOptions)
   at Microsoft.Data.SqlClient.SqlConnection.TryOpen(System.Threading.Tasks.TaskCompletionSource`1<Microsoft.Data.ProviderBase.DbConnectionInternal>, 
Microsoft.Data.SqlClient.SqlConnectionOverrides)
   at Microsoft.Data.SqlClient.SqlConnection.Open(Microsoft.Data.SqlClient.SqlConnectionOverrides)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.OpenDbConnection(Boolean)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternal(Boolean)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator+<>c__DisplayClass18_0.<Exists>b__0(System.DateTime)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions+<>c__DisplayClass12_0`2[[System.DateTime, System.Private.CoreLib, Version=7.0.0.0, 
Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Boolean, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, 
PublicKeyToken=7cec85d7bea7798e]].<Execute>b__0(Microsoft.EntityFrameworkCore.DbContext, System.DateTime)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[[System.DateTime, System.Private.CoreLib, 
Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Boolean, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, 
PublicKeyToken=7cec85d7bea7798e]](System.DateTime, System.Func`3<Microsoft.EntityFrameworkCore.DbContext,System.DateTime,Boolean>, 
System.Func`3<Microsoft.EntityFrameworkCore.DbContext,System.DateTime,Microsoft.EntityFrameworkCore.Storage.ExecutionResult`1<Boolean>>)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[[System.DateTime, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, 
PublicKeyToken=7cec85d7bea7798e],[System.Boolean, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, 
PublicKeyToken=7cec85d7bea7798e]](Microsoft.EntityFrameworkCore.Storage.IExecutionStrategy, System.DateTime, System.Func`2<System.DateTime,Boolean>, 
System.Func`2<System.DateTime,Microsoft.EntityFrameworkCore.Storage.ExecutionResult`1<Boolean>>)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists(Boolean)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(System.String)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(System.String, System.String, System.String)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(System.String, System.String, System.String)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor+UpdateDatabase+<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor+OperationBase.Execute(System.Action)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor+UpdateDatabase..ctor(Microsoft.EntityFrameworkCore.Design.OperationExecutor, 
Microsoft.EntityFrameworkCore.Design.IOperationResultHandler, System.Collections.IDictionary)
   at System.RuntimeMethodHandle.InvokeMethod(System.Object, Void**, System.Signature, Boolean)
   at System.Reflection.ConstructorInvoker.Invoke(System.Object, IntPtr*, System.Reflection.BindingFlags)
   at System.Reflection.RuntimeConstructorInfo.Invoke(System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], 
System.Globalization.CultureInfo)
   at System.RuntimeType.CreateInstanceImpl(System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)
   at System.Activator.CreateInstance(System.Type, System.Object[])
   at Microsoft.EntityFrameworkCore.Tools.ReflectionOperationExecutor.Execute(System.String, System.Object, System.Collections.IDictionary)
   at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid4[[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, 
PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, 
PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, 
PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, 
PublicKeyToken=7cec85d7bea7798e]](System.Runtime.CompilerServices.CallSite, System.__Canon, System.__Canon, System.__Canon, System.__Canon)
   at Microsoft.EntityFrameworkCore.Tools.OperationExecutorBase.InvokeOperationImpl(System.String, System.Collections.IDictionary)
   at Microsoft.EntityFrameworkCore.Tools.OperationExecutorBase.InvokeOperation(System.String, System.Collections.IDictionary)
   at Microsoft.EntityFrameworkCore.Tools.OperationExecutorBase.UpdateDatabase(System.String, System.String, System.String)
   at Microsoft.EntityFrameworkCore.Tools.Commands.DatabaseUpdateCommand.Execute(System.String[])
   at Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase+<>c__DisplayClass0_0.<Configure>b__0(System.String[])
   at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(System.String[])
   at Microsoft.EntityFrameworkCore.Tools.Program.Main(System.String[])
dotnet ef database update --verbose
Using project 'D:\Users\ojesc\Desktop\csl\csl.csproj'.
Using startup project 'D:\Users\ojesc\Desktop\csl\csl.csproj'.
Writing 'D:\Users\ojesc\Desktop\csl\obj\csl.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\ojesc\AppData\Local\Temp\tmp355E.tmp /verbosity:quiet /nologo D:\Users\ojesc\Desktop\csl\csl.csproj
Writing 'D:\Users\ojesc\Desktop\csl\obj\csl.csproj.EntityFrameworkCore.targets'...
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\ojesc\AppData\Local\Temp\tmp38DA.tmp /verbosity:quiet /nologo D:\Users\ojesc\Desktop\csl\csl.csproj
Build started...
dotnet build D:\Users\ojesc\Desktop\csl\csl.csproj /verbosity:quiet /nologo
D:\Users\ojesc\Desktop\csl\TestDbContext.cs(12,23): warning CS8618: Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. [D:\Users\ojesc\Desktop\csl\csl.csproj]

Build succeeded.

D:\Users\ojesc\Desktop\csl\TestDbContext.cs(12,23): warning CS8618: Non-nullable property 'Name' must contain a non-null value when exiting constructor. Consider declaring the property as nullable. [D:\Users\ojesc\Desktop\csl\csl.csproj]
    1 Warning(s)
    0 Error(s)

Time Elapsed 00:00:02.49

Workload updates are available. Run `dotnet workload list` for more information.
Build succeeded.
dotnet exec --depsfile D:\Users\ojesc\Desktop\csl\bin\Debug\net7.0\csl.deps.json --additionalprobingpath C:\Users\ojesc\.nuget\packages --additionalprobingpath "D:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --additionalprobingpath "C:\Program Files (x86)\Microsoft\Xamarin\NuGet" --runtimeconfig D:\Users\ojesc\Desktop\csl\bin\Debug\net7.0\csl.runtimeconfig.json C:\Users\ojesc\.dotnet\tools\.store\dotnet-ef\7.0.3\dotnet-ef\7.0.3\tools\net6.0\any\tools\netcoreapp2.0\any\ef.dll database update --assembly D:\Users\ojesc\Desktop\csl\bin\Debug\net7.0\csl.dll --project D:\Users\ojesc\Desktop\csl\csl.csproj --startup-assembly D:\Users\ojesc\Desktop\csl\bin\Debug\net7.0\csl.dll --startup-project D:\Users\ojesc\Desktop\csl\csl.csproj --project-dir D:\Users\ojesc\Desktop\csl\ --root-namespace csl --language C# --framework net7.0 --nullable --working-dir D:\Users\ojesc\Desktop\csl --verbose
Using assembly 'csl'.
Using startup assembly 'csl'.
Using application base 'D:\Users\ojesc\Desktop\csl\bin\Debug\net7.0'.
Using working directory 'D:\Users\ojesc\Desktop\csl'.
Using root namespace 'csl'.
Using project directory 'D:\Users\ojesc\Desktop\csl\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'csl'...
Finding Microsoft.Extensions.Hosting service provider...
Using environment 'Development'.
Using application service provider from Microsoft.Extensions.Hosting.
Using connection string Data Source=.;Initial Catalog=TestDb;MultipleActiveResultSets=True;Integrated Security=True;User Instance=True
Found DbContext 'TestDbContext'.
Finding DbContext classes in the project...
Using context 'TestDbContext'.
Finding design-time services referenced by assembly 'csl'...
Finding design-time services referenced by assembly 'csl'...
No referenced design-time services were found.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'csl'...
No design-time services were found.
dotnet : Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is 
corrupt.
At line:1 char:1
+ dotnet ef database update --verbose
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Fatal error. Sy...ory is corrupt.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Repeat 2 times:
--------------------------------
   at Microsoft.Data.SqlClient.SNINativeMethodWrapper.SNIOpenSyncExWrapper(SNI_CLIENT_CONSUMER_INFO ByRef, IntPtr ByRef)
--------------------------------
   at Microsoft.Data.SqlClient.SNINativeMethodWrapper.SNIOpenSyncEx(ConsumerInfo, System.String, IntPtr ByRef, Byte[], Byte[], Boolean, Boolean, Int32, 
Boolean, Microsoft.Data.SqlClient.SqlConnectionIPAddressPreference, Microsoft.Data.SqlClient.SQLDNSInfo, System.String)
   at Microsoft.Data.SqlClient.SNIHandle..ctor(ConsumerInfo, System.String, Byte[], Boolean, Int32, Byte[] ByRef, Boolean, Boolean, Boolean, 
Microsoft.Data.SqlClient.SqlConnectionIPAddressPreference, Microsoft.Data.SqlClient.SQLDNSInfo, Boolean, System.String)
   at Microsoft.Data.SqlClient.TdsParserStateObjectNative.CreatePhysicalSNIHandle(System.String, Boolean, Int64, Byte[] ByRef, Byte[][] ByRef, Boolean, 
Boolean, Boolean, Microsoft.Data.SqlClient.SqlConnectionIPAddressPreference, System.String, Microsoft.Data.SqlClient.SQLDNSInfo ByRef, System.String, 
Boolean, Boolean, System.String)
   at Microsoft.Data.SqlClient.TdsParser.Connect(Microsoft.Data.SqlClient.ServerInfo, Microsoft.Data.SqlClient.SqlInternalConnectionTds, Boolean, Int64, 
Microsoft.Data.SqlClient.SqlConnectionString, Boolean)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(Microsoft.Data.SqlClient.ServerInfo, System.String, 
System.Security.SecureString, Boolean, Microsoft.Data.ProviderBase.TimeoutTimer, Boolean)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(Microsoft.Data.SqlClient.ServerInfo, System.String, 
System.Security.SecureString, Boolean, Microsoft.Data.SqlClient.SqlConnectionString, Microsoft.Data.SqlClient.SqlCredential, 
Microsoft.Data.ProviderBase.TimeoutTimer)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(Microsoft.Data.ProviderBase.TimeoutTimer, 
Microsoft.Data.SqlClient.SqlConnectionString, Microsoft.Data.SqlClient.SqlCredential, System.String, System.Security.SecureString, Boolean)
   at Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(Microsoft.Data.ProviderBase.DbConnectionPoolIdentity, 
Microsoft.Data.SqlClient.SqlConnectionString, Microsoft.Data.SqlClient.SqlCredential, System.Object, System.String, System.Security.SecureString, 
Boolean, Microsoft.Data.SqlClient.SqlConnectionString, Microsoft.Data.SqlClient.SessionData, Boolean, System.String, 
Microsoft.Data.ProviderBase.DbConnectionPool)
   at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(Microsoft.Data.Common.DbConnectionOptions, 
Microsoft.Data.Common.DbConnectionPoolKey, System.Object, Microsoft.Data.ProviderBase.DbConnectionPool, System.Data.Common.DbConnection, 
Microsoft.Data.Common.DbConnectionOptions)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(Microsoft.Data.ProviderBase.DbConnectionPool, 
System.Data.Common.DbConnection, Microsoft.Data.Common.DbConnectionOptions, Microsoft.Data.Common.DbConnectionPoolKey, 
Microsoft.Data.Common.DbConnectionOptions)
   at Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(System.Data.Common.DbConnection, Microsoft.Data.Common.DbConnectionOptions, 
Microsoft.Data.ProviderBase.DbConnectionInternal)
   at Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(System.Data.Common.DbConnection, Microsoft.Data.Common.DbConnectionOptions, 
Microsoft.Data.ProviderBase.DbConnectionInternal)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(System.Data.Common.DbConnection, UInt32, Boolean, Boolean, 
Microsoft.Data.Common.DbConnectionOptions, Microsoft.Data.ProviderBase.DbConnectionInternal ByRef)
   at Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(System.Data.Common.DbConnection, 
System.Threading.Tasks.TaskCompletionSource`1<Microsoft.Data.ProviderBase.DbConnectionInternal>, Microsoft.Data.Common.DbConnectionOptions, 
Microsoft.Data.ProviderBase.DbConnectionInternal ByRef)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(System.Data.Common.DbConnection, 
System.Threading.Tasks.TaskCompletionSource`1<Microsoft.Data.ProviderBase.DbConnectionInternal>, Microsoft.Data.Common.DbConnectionOptions, 
Microsoft.Data.ProviderBase.DbConnectionInternal, Microsoft.Data.ProviderBase.DbConnectionInternal ByRef)
   at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(System.Data.Common.DbConnection, 
Microsoft.Data.ProviderBase.DbConnectionFactory, System.Threading.Tasks.TaskCompletionSource`1<Microsoft.Data.ProviderBase.DbConnectionInternal>, 
Microsoft.Data.Common.DbConnectionOptions)
   at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(System.Data.Common.DbConnection, Microsoft.Data.ProviderBase.DbConnectionFactory, 
System.Threading.Tasks.TaskCompletionSource`1<Microsoft.Data.ProviderBase.DbConnectionInternal>, Microsoft.Data.Common.DbConnectionOptions)
   at Microsoft.Data.SqlClient.SqlConnection.TryOpen(System.Threading.Tasks.TaskCompletionSource`1<Microsoft.Data.ProviderBase.DbConnectionInternal>, 
Microsoft.Data.SqlClient.SqlConnectionOverrides)
   at Microsoft.Data.SqlClient.SqlConnection.Open(Microsoft.Data.SqlClient.SqlConnectionOverrides)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.OpenDbConnection(Boolean)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenInternal(Boolean)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator+<>c__DisplayClass18_0.<Exists>b__0(System.DateTime)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions+<>c__DisplayClass12_0`2[[System.DateTime, System.Private.CoreLib, Version=7.0.0.0, 
Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Boolean, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, 
PublicKeyToken=7cec85d7bea7798e]].<Execute>b__0(Microsoft.EntityFrameworkCore.DbContext, System.DateTime)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[[System.DateTime, System.Private.CoreLib, 
Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Boolean, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, 
PublicKeyToken=7cec85d7bea7798e]](System.DateTime, System.Func`3<Microsoft.EntityFrameworkCore.DbContext,System.DateTime,Boolean>, 
System.Func`3<Microsoft.EntityFrameworkCore.DbContext,System.DateTime,Microsoft.EntityFrameworkCore.Storage.ExecutionResult`1<Boolean>>)
   at Microsoft.EntityFrameworkCore.ExecutionStrategyExtensions.Execute[[System.DateTime, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, 
PublicKeyToken=7cec85d7bea7798e],[System.Boolean, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, 
PublicKeyToken=7cec85d7bea7798e]](Microsoft.EntityFrameworkCore.Storage.IExecutionStrategy, System.DateTime, System.Func`2<System.DateTime,Boolean>, 
System.Func`2<System.DateTime,Microsoft.EntityFrameworkCore.Storage.ExecutionResult`1<Boolean>>)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists(Boolean)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(System.String)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(System.String, System.String, System.String)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(System.String, System.String, System.String)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor+UpdateDatabase+<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor+OperationBase.Execute(System.Action)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor+UpdateDatabase..ctor(Microsoft.EntityFrameworkCore.Design.OperationExecutor, 
Microsoft.EntityFrameworkCore.Design.IOperationResultHandler, System.Collections.IDictionary)
   at System.RuntimeMethodHandle.InvokeMethod(System.Object, Void**, System.Signature, Boolean)
   at System.Reflection.ConstructorInvoker.Invoke(System.Object, IntPtr*, System.Reflection.BindingFlags)
   at System.Reflection.RuntimeConstructorInfo.Invoke(System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], 
System.Globalization.CultureInfo)
   at System.RuntimeType.CreateInstanceImpl(System.Reflection.BindingFlags, System.Reflection.Binder, System.Object[], System.Globalization.CultureInfo)
   at System.Activator.CreateInstance(System.Type, System.Object[])
   at Microsoft.EntityFrameworkCore.Tools.ReflectionOperationExecutor.Execute(System.String, System.Object, System.Collections.IDictionary)
   at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid4[[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, 
PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, 
PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, 
PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, 
PublicKeyToken=7cec85d7bea7798e]](System.Runtime.CompilerServices.CallSite, System.__Canon, System.__Canon, System.__Canon, System.__Canon)
   at Microsoft.EntityFrameworkCore.Tools.OperationExecutorBase.InvokeOperationImpl(System.String, System.Collections.IDictionary)
   at Microsoft.EntityFrameworkCore.Tools.OperationExecutorBase.InvokeOperation(System.String, System.Collections.IDictionary)
   at Microsoft.EntityFrameworkCore.Tools.OperationExecutorBase.UpdateDatabase(System.String, System.String, System.String)
   at Microsoft.EntityFrameworkCore.Tools.Commands.DatabaseUpdateCommand.Execute(System.String[])
   at Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase+<>c__DisplayClass0_0.<Configure>b__0(System.String[])
   at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(System.String[])
   at Microsoft.EntityFrameworkCore.Tools.Program.Main(System.String[])

Running the application results in the same exception.

ErikEJ commented 1 year ago

@sake402 You connection string looks odd, do you really mean "user instance=true" and if so, I think you are missing:

AttachDBFilename

https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/sql/sql-server-express-user-instances

lcheunglci commented 1 year ago

Hi @sake402, usually an exception from SNI Native with a memory access violation could potential mean there's a mismatch between a compatible version of Microsoft.Data.SqlClient.SNI and Microsoft.Data.SqlClient specifically for Windows. From what I can tell so far, between EF Core 6 and EF Core 7 is that EF Core 6 uses Microsoft.Data.SqlClient version 2.1.4 and EF Core 7 uses Microsoft.Data.SqlClient version 5.0.1 and when upgrading between those version, in the documentation, it also mentions breaking changes as well. Also, as @ErikEJ mentioned, there's potentially a missing property in the connection string. Is there a sample repro project that you can provide? or are you able to try it with the missing property AttachDbFileName or remove the user instance=true property and see if this issue repros?

sake402 commented 1 year ago

@ErikEJ Thanks for your response. I was trying a number of combination as it confuses me the source of the error. So I happen to have left that fragment there.

At your words, I however tested again with

  "ConnectionStrings": {
    "DefaultConnection": "Data Source=.;AttachDbFileName=C:\\TestDb.mdf;MultipleActiveResultSets=True;Integrated Security=True;User Instance=True",
  },

and also

  "ConnectionStrings": {
    "DefaultConnection": "Data Source=.;Initial Catalog=TestDb;MultipleActiveResultSets=True;Integrated Security=True"
  },

Same result. The only that works is if I downgrade to EF6.

sake402 commented 1 year ago

@lcheunglci Thank for your response. I thought as much too and even tried installing Microsoft.Data.SqlClient 5.1.0, but got the same response.

JRahnama commented 1 year ago

@sake402 can you confirm that SNI version is accurate? We are looking for Microsoft.Data.SqlClient.SNI package. Each version of M.D.SqlClient has a dependency to a specific version of SNI. You can clean your project and build it again with dependency to M.D.SqlClient v5.1.0. Make sure that you have M.D.SqlClient.SNI or M.D.SqlClient.SNI.runtime with version of 5.1.0 as well.

ErikEJ commented 1 year ago

Please provide a repro project.

emidah commented 1 year ago

I have the same issue. "Repro":

.csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.1" />
  </ItemGroup>

</Project>

Program.cs:

using Microsoft.Data.SqlClient;

var connectionString = "data source=.\\SQLEXPRESS;Integrated Security=true;AttachDBFilename=|DataDirectory|\\db.mdf;User Instance=true";
using var connection = new SqlConnection();
connection.ConnectionString = connectionString;
connection.Open();

Note that it doesn't actually matter if the database server at .\SQLEXPRESS actually exists, it doesn't even matter if the mdf file exists, simply any attempt at connecting to any server with "User Instance=true" results in access violation every single time

SqlClient is 5.1.1 and SNI runtime 5.1.0. Platform is x64 windows

I don't really care about this SQL Server feature, it's just that access violation exceptions are super nasty since they are not recoverable.

The issue started for us when we upgraded from 4.1.0 to 5.1.1

I ran a bisect on this with my repro and it broke at commit 778f86e5 aka #1608

emidah commented 1 year ago

I found the issue and will send a one-liner pull request shortly...

NielsFilter commented 1 year ago

I'm experiencing the same after upgrading Microsoft.Data.SqlClient to 5.1.1. But only see it when running with Azure Function. If I use the class library from an Asp.net application