简体   繁体   中英

Entity Framework Core SaveChanges causes error on Mac

I'm currently trying to build an EF core project, but when I call db.SaveChanges() I get the following error:

System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.InOutOfProcHelper' threw an exception. ---> System.EntryPointNotFoundException: GetModuleHandle assembly:<unknown assembly> type:<unknown type> member:(null)
  at (wrapper managed-to-native) Microsoft.Data.Common.SafeNativeMethods.GetModuleHandle(string)
  at Microsoft.Data.SqlClient.InOutOfProcHelper..ctor () [0x00006] in <a1c8e16fd44948ee8c6a463f125e71f0>:0 
  at Microsoft.Data.SqlClient.InOutOfProcHelper..cctor () [0x00000] in <a1c8e16fd44948ee8c6a463f125e71f0>:0 
   --- End of inner exception stack trace ---
  at Microsoft.Data.SqlClient.SqlConnectionString..ctor (System.String connectionString) [0x0000d] in <a1c8e16fd44948ee8c6a463f125e71f0>:0 
  at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions (System.String connectionString, Microsoft.Data.Common.DbConnectionOptions previous) [0x00000] in <a1c8e16fd44948ee8c6a463f125e71f0>:0 
  at Microsoft.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup (Microsoft.Data.Common.DbConnectionPoolKey key, Microsoft.Data.ProviderBase.DbConnectionPoolGroupOptions poolOptions, Microsoft.Data.Common.DbConnectionOptions& userConnectionOptions) [0x0003e] in <a1c8e16fd44948ee8c6a463f125e71f0>:0 
  at Microsoft.Data.SqlClient.SqlConnection.ConnectionString_Set (Microsoft.Data.Common.DbConnectionPoolKey key) [0x00008] in <a1c8e16fd44948ee8c6a463f125e71f0>:0 
  at Microsoft.Data.SqlClient.SqlConnection.set_ConnectionString (System.String value) [0x0006b] in <a1c8e16fd44948ee8c6a463f125e71f0>:0 
  at Microsoft.Data.SqlClient.SqlConnection..ctor (System.String connectionString, Microsoft.Data.SqlClient.SqlCredential credential) [0x00006] in <a1c8e16fd44948ee8c6a463f125e71f0>:0 
  at Microsoft.Data.SqlClient.SqlConnection..ctor (System.String connectionString) [0x00000] in <a1c8e16fd44948ee8c6a463f125e71f0>:0 
  at (wrapper remoting-invoke-with-check) Microsoft.Data.SqlClient.SqlConnection..ctor(string)
  at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.CreateDbConnection () [0x00006] in <5c775909c1cf40629fd2452310cf8cc1>:0 
  at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection () [0x0000a] in <5314a29febc14288a1bc53bf7b46d725>:0 
  at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.CreateCommand (Microsoft.EntityFrameworkCore.Storage.RelationalCommandParameterObject parameterObject, System.Guid commandId, Microsoft.EntityFrameworkCore.Diagnostics.DbCommandMethod commandMethod) [0x0005c] in <5314a29febc14288a1bc53bf7b46d725>:0 
  at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader (Microsoft.EntityFrameworkCore.Storage.RelationalCommandParameterObject parameterObject) [0x00027] in <5314a29febc14288a1bc53bf7b46d725>:0 
  at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1+Enumerator[T].InitializeReader (Microsoft.EntityFrameworkCore.DbContext _, System.Boolean result) [0x00050] in <5314a29febc14288a1bc53bf7b46d725>:0 
  at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult] (TState state, System.Func`3[T1,T2,TResult] operation, System.Func`3[T1,T2,TResult] verifySucceeded) [0x00011] in <5c775909c1cf40629fd2452310cf8cc1>:0 
  at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1+Enumerator[T].MoveNext () [0x00193] in <5314a29febc14288a1bc53bf7b46d725>:0 
  at System.Linq.Enumerable.SingleOrDefault[TSource] (System.Collections.Generic.IEnumerable`1[T] source) [0x00041] in /Users/builder/jenkins/workspace/build-package-osx-mono/2019-10/external/bockbuild/builds/mono-x64/external/corefx/src/System.Linq/src/System/Linq/Single.cs:104 
  at (wrapper dynamic-method) System.Object.lambda_method(System.Runtime.CompilerServices.Closure,Microsoft.EntityFrameworkCore.Query.QueryContext)
  at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult] (System.Linq.Expressions.Expression query) [0x00075] in <a3dbe48366454fdbb51dfeeb0b5c4997>:0 
  at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult] (System.Linq.Expressions.Expression expression) [0x00000] in <a3dbe48366454fdbb51dfeeb0b5c4997>:0 
  at System.Linq.Queryable.FirstOrDefault[TSource] (System.Linq.IQueryable`1[T] source) [0x0000e] in /Users/builder/jenkins/workspace/build-package-osx-mono/2019-10/external/bockbuild/builds/mono-x64/external/corefx/src/System.Linq.Queryable/src/System/Linq/Queryable.cs:729 
  at GenericEFIAPI.RTATenantsDatabaseHelper.convertTenant (System.String tenantId, GenericEFIAPI.configObject config, GenericEFIAPI.models.TenantDatabase db) [0x002d2] in /Users/jft/projects/GenericAPI/GenericEFIAPI/RTATenantsDatabaseHelper.cs:108 
  at GenericEFIAPI.Program+<>c__DisplayClass1_0.<Main>b__0 (GenericEFIAPI.Program+Options o) [0x000c1] in /Users/jft/projects/GenericAPI/GenericEFIAPI/Program.cs:78 
  at CommandLine.ParserResultExtensions.WithParsed[T] (CommandLine.ParserResult`1[T] result, System.Action`1[T] action) [0x00011] in <68c69d6559fd42bc897efd2a474dfda9>:0 
  at GenericEFIAPI.Program.Main (System.String[] args) [0x00013] in /Users/jft/projects/GenericAPI/GenericEFIAPI/Program.cs:38 

I've tried looking around on google for some help, but have come up empty so far... Anybody have any ideas? I'm trying to connect to sql server and using DbContextOptionsBuilder().UseSqlServer() for the underlying connection.

I'm currently getting my old windows laptop up and running to see if it is because I'm on a Mac or not, but I thought I would get this question out there in the mean time.

Alright. So I'm not 100% sure why my connection string is failing when passing it directly to the DbContextOptionsBuilder(),UseSqlServer(connectionString), but if I create a SqlConnection object before that, and pass that as the parameter to UseSqlServer. it works.

So got a workaround, but not sure why the error happens when just passing the connection string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM