1

I try to add something to my dbcontext asynchronously using the following code:

var entry = await _context.AddAsync(person);

However, I get the following exception:

System.ArgumentNullException: Value cannot be null. Parameter name: key

at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)

at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)

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

at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.TryGetEntry(Object entity, IEntityType entityType)

at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.InitialFixup(InternalEntityEntry entry, ISet`1 handledForeignKeys, Boolean fromQuery)

at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.NavigationFixer.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)

at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.InternalEntityEntryNotifier.StateChanged(InternalEntityEntry entry, EntityState oldState, Boolean fromQuery)

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

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

--- End of stack trace from previous location where exception was thrown ---

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

at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

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

--- End of stack trace from previous location where exception was thrown ---

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

at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

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

--- End of stack trace from previous location where exception was thrown ---

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

at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

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

--- End of stack trace from previous location where exception was thrown ---

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

at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

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

--- End of stack trace from previous location where exception was thrown ---

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

at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

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

at PersonRepository.d__4.MoveNext()

--- End of stack trace from previous location where exception was thrown ---

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

at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

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

at PersonController.d__3.MoveNext() in PersonController.cs:line 63

While reading these statements I couldn't find which value(s) I could be missing. And while debugging I don't know what to look for. How can I find which values I'm missing while debugging?

Friso
  • 2,328
  • 9
  • 36
  • 72

1 Answers1

1

It looks like the ID defined for the person you are trying to add is null. If you don't have a database ID generation configured, then you'll need to supply an ID for the person before you add it to the context.

keithwill
  • 1,964
  • 1
  • 17
  • 26
  • 1
    See https://learn.microsoft.com/en-us/ef/core/modeling/keys for more info on defining a key column for your model (as in a primary key column(s)). – jdnew18 Jan 24 '19 at 15:58