我正在构建一个具有EF Core 2.2.x
和EntityFrameworkCore.Jet
的.NET Core 3
控制台应用程序,以从/向访问数据库(mdb(执行一些通用读取和写入。正如文档所说,我正在为x86
编译。目标是将丢失的大量数据从旧系统迁移到较新的系统(同步数据(。
我已经搭建了数据库的基架,我可以完美地从数据库中读取,但是一旦我去更新实体,我就会收到访问冲突异常:
代码示例(为简洁起见,已缩短(:
public void SetSynced(int id)
{
var item = _db.Products.Where(x => x.Id == id).Single();
item.NeedsSyncing = false;
_db.SaveChanges();
}
命中此代码时,将显示以下异常(无内部异常(
System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
我不明白为什么会发生这种情况,因为我可以完美地读取数据库,它只在写入时执行此操作。
我尝试使用以下连接字符串(驱动程序(,但两者都具有相同的最终结果:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\database.mdb;Jet OLEDB:Database Password = password;`
Provider=Microsoft.jet.oledb.4.0;Data Source=C:\database.mdb;Jet OLEDB:Database Password = password;
在x64
阅读工作上运行时,但我得到以下异常:
System.Data.OleDb.OleDbException (0x80004002): No error message available, result code: E_NOINTERFACE(0x80004002).
我迷路了,在黑暗中寻找。有没有人对我去哪里寻找有经验/想法?
更新 02/02/2020: 我尝试使用 OleDb 手动连接并运行查询以查看将我带到哪里。下面的代码有效。当我用上面的代码将其切换出来时,它再次失败。
db = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\database.mdb;Jet OLEDB:Database Password = password");
db.Open();
var command = new OleDbCommand($"UPDATE Products SET NeedsSyncing = 0 WHERE Id= 1", db);
var res = command.ExecuteNonQuery();
设置JetConfiguration.ShowSqlStatements = true
后,我得到了以下堆栈跟踪:
ExecuteDbDataReader==========
UPDATE [Products] SET [PictureName] = @p0
WHERE [Id] = @p1;
SELECT @@ROWCOUNT;
@p1(Int32) = 1
@p0(String) = 'notfound.jpg'
Fatal error. 0xC0000005
at System.Data.Common.UnsafeNativeMethods+ICommandWithParameters.SetParameterInfo(IntPtr, IntPtr[], System.Data.OleDb.tagDBPARAMBINDINFO[])
at System.Data.OleDb.OleDbCommand.ApplyParameterBindings(ICommandWithParameters, System.Data.OleDb.tagDBPARAMBINDINFO[])
at System.Data.OleDb.OleDbCommand.CreateAccessor()
at System.Data.OleDb.OleDbCommand.InitializeCommand(System.Data.CommandBehavior, Boolean)
at System.Data.OleDb.OleDbCommand.ExecuteCommand(System.Data.CommandBehavior, System.Object ByRef)
at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(System.Data.CommandBehavior, System.String)
at System.Data.OleDb.OleDbCommand.ExecuteReader(System.Data.CommandBehavior)
at System.Data.OleDb.OleDbCommand.ExecuteDbDataReader(System.Data.CommandBehavior)
at System.Data.Common.DbCommand.ExecuteReader(System.Data.CommandBehavior)
at System.Data.Jet.JetCommand.InternalExecuteDbDataReader(System.String, System.Data.CommandBehavior)
at System.Data.Jet.JetCommand.ExecuteDbDataReader(System.Data.CommandBehavior)
at System.Data.Common.DbCommand.ExecuteReader()
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(Microsoft.EntityFrameworkCore.Storage.IRelationalConnection, Microsoft.EntityFrameworkCore.Diagnostics.DbCommandMethod, System.Collections.Generic.IReadOnlyDictionary`2<System.String,System.Object>)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(Microsoft.EntityFrameworkCore.Storage.IRelationalConnection, System.Collections.Generic.IReadOnlyDictionary`2<System.String,System.Object>)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.Execute(Microsoft.EntityFrameworkCore.Storage.IRelationalConnection)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(Microsoft.EntityFrameworkCore.DbContext, System.ValueTuple`2<System.Collections.Generic.IEnumerable`1<Microsoft.EntityFrameworkCore.Update.ModificationCommandBatch>,Microsoft.EntityFrameworkCore.Storage.IRelationalConnection>)
at Microsoft.EntityFrameworkCore.Storage.Internal.NoopExecutionStrategy.Execute[[System.ValueTuple`2[[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.__Canon, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Int32, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]](System.ValueTuple`2<System.__Canon,System.__Canon>, System.Func`3<Microsoft.EntityFrameworkCore.DbContext,System.ValueTuple`2<System.__Canon,System.__Canon>,Int32>, System.Func`3<Microsoft.EntityFrameworkCore.DbContext,System.ValueTuple`2<System.__Canon,System.__Canon>,Microsoft.EntityFrameworkCore.Storage.ExecutionResult`1<Int32>>)
at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.Execute(System.Collections.Generic.IEnumerable`1<Microsoft.EntityFrameworkCore.Update.ModificationCommandBatch>, Microsoft.EntityFrameworkCore.Storage.IRelationalConnection)
at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(System.Collections.Generic.IReadOnlyList`1<Microsoft.EntityFrameworkCore.Update.IUpdateEntry>)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(System.Collections.Generic.IReadOnlyList`1<Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry>)
at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean)
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean)
at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()
at Cashmaster.BackgroundWorker.Services.CashMasterService.Crash()
Nuget 软件包 + 版本(与 Jet 相关(:
Microsoft.EntityFrameworkCore 2.2.6
EntityFrameworkCore.Jet 2.2.0
System.Data.OleDb 4.7.0
在.NET Core 3.1
上运行这一切。
该问题在GitHub上由将x86特定结构迁移到AnyCPU for System.Data.OleDb #32509跟踪,并通过修复System.Data.OleDb #33899中的x86打包问题由社区成员修复
。若要修复项目中的问题,请使用最新版本的 EF Core 3.1.x(当前为 3.1.10(和最新版本的EntityFrameworkCore.Jet
(当前预发行版3.1.0-alpha.4(以及System.Data.OleDb
版本 5.0.0。