定位调用栈未显示的代码 解决DevExpress EFCore空引用异常
运行时触发空引用异常,具体错误信息如下:
System.NullReferenceException HResult=0x80004003 错误信息: 未将对象引用设置到对象的实例。 错误来源: DevExpress.Persistent.BaseImpl.EFCore.v22.1 堆栈跟踪: at DevExpress.Persistent.BaseImpl.EFCore.AuditTrail.AuditDataItem.UpdateDefaultString(IEFCoreWeakReference objectForUpdate, EntityEntry entityEntry, Object key) in DevExpress.Persistent\DevExpress.Persistent.BaseImpl.EFCore\AuditTrail\AuditDataItem.cs:line 99
完整调用栈
DevExpress.Persistent.BaseImpl.EFCore.v22.1.dll!DevExpress.Persistent.BaseImpl.EFCore.AuditTrail.AuditDataItem.UpdateDefaultString(DevExpress.Persistent.BaseImpl.EFCore.IEFCoreWeakReference objectForUpdate, Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry entityEntry, object key) 行 99 C#
DevExpress.Persistent.BaseImpl.EFCore.v22.1.dll!DevExpress.Persistent.BaseImpl.EFCore.AuditTrail.AuditDataItem.UpdatePersistentData(System.Collections.Generic.List<DevExpress.Persistent.BaseImpl.EFCore.IEFCoreWeakReference> weakReferences) 行 63 C#
DevExpress.Persistent.BaseImpl.EFCore.v22.1.dll!DevExpress.Persistent.BaseImpl.EFCore.AuditTrail.AuditDataContainer.AttachAuditData(Microsoft.EntityFrameworkCore.DbContext dbContext) 行 73 C#
DevExpress.Persistent.BaseImpl.EFCore.v22.1.dll!DevExpress.Persistent.BaseImpl.EFCore.AuditTrail.AuditDataContainer.SaveAuditData(Microsoft.EntityFrameworkCore.DbContext dbContext) 行 59 C#
DevExpress.Persistent.BaseImpl.EFCore.v22.1.dll!DevExpress.Persistent.BaseImpl.EFCore.AuditTrail.AuditTrailService.AuditedDbContext_SavedChanges(object sender, Microsoft.EntityFrameworkCore.SavedChangesEventArgs e) 行 142 C#
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.DbContext.SaveChanges(bool acceptAllChangesOnSuccess) 行 515 C#
MyApp.Module.dll!MyApp.Module.BusinessObjects.JobTalkDbContext.SaveChanges() 行 466 C#
DevExpress.ExpressApp.EFCore.v22.1.dll!DevExpress.ExpressApp.EFCore.EFCoreObjectSpace.DoCommit() 行 343 C#
DevExpress.EntityFrameworkCore.Security.v22.1.dll!DevExpress.EntityFrameworkCore.Security.SecuredEFCoreObjectSpace.DoCommit() 行 142 C#
DevExpress.ExpressApp.v22.1.dll!DevExpress.ExpressApp.BaseObjectSpace.CommitChanges() 行 370 C#
MyApp.Module.Win.dll!MyApp.Module.Win.Features.PurchFeature.POStateViewController.actPurchStateMenu_Execute(object sender, DevExpress.ExpressApp.Actions.SingleChoiceActionExecuteEventArgs args) 行 200 C#
DevExpress.ExpressApp.v22.1.dll!DevExpress.ExpressApp.Actions.ActionBase.ExecuteCore(System.Delegate handler, DevExpress.ExpressApp.Actions.ActionBaseEventArgs eventArgs) 行 289 C#
DevExpress.ExpressApp.v22.1.dll!DevExpress.ExpressApp.Actions.SingleChoiceAction.DoExecute(DevExpress.ExpressApp.Actions.ChoiceActionItem selectedItem) 行 111 C#
DevExpress.ExpressApp.v22.1.dll!DevExpress.ExpressApp.Templates.ActionControls.Binding.SingleChoiceActionBinding.ActionControl_Execute(object sender, DevExpress.ExpressApp.Templates.ActionControls.SingleChoiceActionControlExecuteEventArgs e) 行 89 C#
DevExpress.XtraBars.v22.1.dll!DevExpress.XtraBars.BarItem.OnClick(DevExpress.XtraBars.BarItemLink link) 行 1219 C#
DevExpress.XtraBars.v22.1.dll!DevExpress.XtraBars.BarItemLink.OnLinkClick() 行 1222 C#
DevExpress.XtraBars.v22.1.dll!DevExpress.XtraBars.BarButtonItemLink.OnLinkAction(DevExpress.XtraBars.BarLinkAction action, object actionArgs) 行 781 C#
DevExpress.XtraBars.v22.1.dll!DevExpress.XtraBars.ViewInfo.BarSelectionInfo.UnPressLink(DevExpress.XtraBars.BarItemLink link) 行 836 C#
DevExpress.XtraBars.v22.1.dll!DevExpress.XtraBars.Controls.CustomLinksControl.OnMouseUp(System.Windows.Forms.MouseEventArgs e) 行 1173 C#
System.Windows.Forms.dll!System.Windows.Forms.Control.WmMouseUp(ref System.Windows.Forms.Message m, System.Windows.Forms.MouseButtons button, int clicks) 行 12439 C#
System.Windows.Forms.dll!System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m) 行 13229 C#
DevExpress.Utils.v22.1.dll!DevExpress.XtraEditors.XtraControl.WndProc(ref System.Windows.Forms.Message m) 行 149 C#
DevExpress.XtraBars.v22.1.dll!DevExpress.XtraBars.Controls.CustomControl.WndProc(ref System.Windows.Forms.Message msg) 行 414 C#
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m) 行 68 C#
System.Windows.Forms.dll!System.Windows.Forms.Control.ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m) 行 122 C#
System.Windows.Forms.dll!System.Windows.Forms.NativeWindow.Callback(System.IntPtr hWnd, Interop.User32.WM msg, System.IntPtr wparam, System.IntPtr lparam) 行 370 C#
[本机到托管过渡]
[托管到本机过渡]
System.Windows.Forms.dll!System.Windows.Forms.Application.ComponentManager.Interop.Mso.IMsoComponentManager.FPushMessageLoop(System.UIntPtr dwComponentID, Interop.Mso.msoloop uReason, void* pvLoopData) 行 346 C#
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Interop.Mso.msoloop reason, System.Windows.Forms.ApplicationContext context) 行 1117 C#
System.Windows.Forms.dll!System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Interop.Mso.msoloop reason, System.Windows.Forms.ApplicationContext context) 行 981 C#
System.Windows.Forms.dll!System.Windows.Forms.Application.Run() 行 1181 C#
DevExpress.ExpressApp.Win.v22.1.dll!DevExpress.ExpressApp.Win.WinApplication.Start() 行 834 C#
MyApp.Win.dll!MyApp.Win.Program.Main() 行 112 C#
- 已在Visual Studio调试选项中取消勾选启用仅我的代码(Enable Just My Code),并开启DevExpress、Nuget、Microsoft来源的符号加载,但仍无法查看
UpdateDefaultString方法的调用位置,无法定位触发点排查空引用根因。 - 尝试在Visual Studio中查看
AuditTrailService.SaveAuditData的源码时,弹出提示:无法导航到插入符号下的符号。当前开发环境为VS2022 17.2.6,不确定该问题是否由缺失符号文件导致。
排查进展
初步判断错误可能和外键配置错误有关:外键字段存在脏数据,本该存储null的值存成了0,且数据库层没有强制关联约束。
- 在代码第63行添加断点后,观察到
AuditTrialService.GetKeyAsObject被调用,但无法单步进入Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalClrEntityEntry类型。 - 调试时悬停查看
Type类型的entityType变量,弹出如下提示:
entityType = {Name = 当前上下文不支持检查调试进程中 System.Type 类型对象的状态。FullName = 当前上下文不支持检查调试进程中 System.Type 类型对象的状态。}
内容的提问来源于stack exchange,提问作者Kirsten

