dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.49k stars 3.13k forks source link

Error: Value cannot be null parameter name key #33831

Open douglasg14b opened 1 month ago

douglasg14b commented 1 month ago

File a bug

I've read a few issues on this and have been unable to gleam much information from them

Include your code

I'm still trying to figure out what might be triggering this, I do not have a trivial repro at this time. The affecting code is a lot of business logic that, in the end, adds a new related entity, and deleted & adds an owned entity.

// TODO figure out what the problem is

Include stack traces

Include the full exception message and stack trace for any exception you encounter.

Use triple-tick fences for stack traces. For example:

System.ArgumentNullException: Value cannot be null. (Parameter 'key')
   at System.Collections.Generic.Dictionary`2.FindValue(TKey key)
   at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
   at Microsoft.EntityFrameworkCore.Update.ModificationCommand.<GenerateColumnModifications>g__HandleJson|41_4(List`1 columnModifications, <>c__DisplayClass41_0&)
   at Microsoft.EntityFrameworkCore.Update.ModificationCommand.GenerateColumnModifications()
   at Microsoft.EntityFrameworkCore.Update.ModificationCommand.<>c.<get_ColumnModifications>b__33_0(ModificationCommand command)
   at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue& target, TParam param, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Update.ModificationCommand.get_ColumnModifications()
   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.CreateCommandBatches(IEnumerable`1 commandSet, Boolean moreCommandSets, Boolean assertColumnModification, ParameterNameGenerator parameterNameGenerator)+MoveNext()
   at Microsoft.EntityFrameworkCore.Update.Internal.CommandBatchPreparer.BatchCommands(IList`1 entries, IUpdateAdapter updateAdapter)+MoveNext()
   at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList`1 entriesToSave, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(StateManager stateManager, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
   at Buzzscreens.Infrastructure.DbContextHooks.HookableDbContext`3.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken) in C:\Users\Douglas\Documents\01 Programming\02 Contracts\buzzscreens-app\apps\backend\Buzzscreens.Infrastructure\DbContextHooks\HookableDbContext.cs:line 85

Attempts:

  1. Modifying instead of replacing the child owned entity: No Change ❌
  2. Change child owned entity from record to class`: No Change ❌
  3. Change Owned Entity to a record and update it with AdlogicLink with { State = newState } to get a whole new instance. ✅

Include verbose output

No problems here.

Include provider and version information

EF Core version: 8.0.4 Database provider: Postgres Target framework: .NET 8 Operating system: Windows 10 IDE: Rider 2024.1.2

douglasg14b commented 1 month ago

Alright, I managed to make the problem go away by:

  1. Not modifying the child owned entity and replacing the entire owned entity instead

Is this intentional behavior, or a bug?

maumar commented 1 month ago

It's definitely a bug - we should never throw exception like that possibly related to https://github.com/dotnet/efcore/issues/33452

douglasg14b commented 2 weeks ago

Well that's awkward, I remember writing in repro code into this, but upon revisiting this issue I see now such thing in the description. 😬

I must have never clicked save, cripes.