首页 > 解决方案 > 如何在调试时找到我丢失的值?

问题描述

我尝试使用以下代码向我的 dbcontext 异步添加一些内容:

var entry = await _context.AddAsync(person);

但是,我得到以下异常:

System.ArgumentNullException:值不能为空。参数名称:key

在 System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument 参数)

在 System.Collections.Generic.Dictionary`2.FindEntry(TKey 键)

在 System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)

在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.TryGetEntry(对象实体,IEntityType entityType)

在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.InitialFixup(InternalEntityEntry 条目,ISet`1 处理的ForeignKeys,布尔 fromQuery)

在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.StateChanged(InternalEntityEntry 条目,EntityState oldState,布尔 fromQuery)

在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.StateChanged(InternalEntityEntry 条目,EntityState oldState,布尔 fromQuery)

在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.FireStateChanged(EntityState oldState)

在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntry.d__18.MoveNext()

--- 从先前抛出异常的位置结束堆栈跟踪 ---

在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)

在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityGraphAttacher.d__6.MoveNext()

--- 从先前抛出异常的位置结束堆栈跟踪 ---

在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)

在 Microsoft.EntityFrameworkCore.ChangeTracking.Internal.EntityEntryGraphIterator.d__1`1.MoveNext()

--- 从先前抛出异常的位置结束堆栈跟踪 ---

在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)

在 Microsoft.EntityFrameworkCore.DbContext.d__64.MoveNext()

--- 从先前抛出异常的位置结束堆栈跟踪 ---

在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)

在 Microsoft.EntityFrameworkCore.DbContext.d__66`1.MoveNext()

--- 从先前抛出异常的位置结束堆栈跟踪 ---

在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)

在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

在 PersonRepository.d__4.MoveNext()

--- 从先前抛出异常的位置结束堆栈跟踪 ---

在 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)

在 System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

在 PersonController.cs:line 63 中的 PersonController.d__3.MoveNext()

在阅读这些陈述时,我找不到我可能遗漏的值。在调试时我不知道要寻找什么。如何在调试时找到我丢失的值?

标签: c#.netentity-frameworkdebuggingargumentnullexception

解决方案


看起来为您要添加的人定义的 ID 为空。如果您没有配置数据库 ID 生成,则需要在将其添加到上下文之前为该人员提供一个 ID。


推荐阅读