ArgumentNullException when inserting record contains nested objects

Asif A 0 Reputation points
2025-05-08T08:19:51.3266667+00:00

Hello,

I'm getting these error below when trying to insert an object with nested objects (jsonb).

Error:

Unhandled exception. 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 Npgsql.EntityFrameworkCore.PostgreSQL.Update.Internal.NpgsqlUpdateSqlGenerator.AppendInsertOperation(StringBuilder commandStringBuilder, IReadOnlyModificationCommand command, Int32 commandPosition, Boolean overridingSystemValue, Boolean& requiresTransaction)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Update.Internal.NpgsqlUpdateSqlGenerator.AppendInsertOperation(StringBuilder commandStringBuilder, IReadOnlyModificationCommand command, Int32 commandPosition, Boolean& requiresTransaction)
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.AddCommand(IReadOnlyModificationCommand modificationCommand)
   at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.TryAddCommand(IReadOnlyModificationCommand modificationCommand)
   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.Execute(IEnumerable`1 commandBatches, IRelationalConnection connection)
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabase.SaveChanges(IList`1 entries)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(IList`1 entriesToSave)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(StateManager stateManager, Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.<>c.<SaveChanges>b__112_0(DbContext _, ValueTuple`2 t)
   at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
   at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges(Boolean acceptAllChangesOnSuccess)
   at Microsoft.EntityFrameworkCore.DbContext.SaveChanges()

Code:

// I'm using Npgsql.EntityFrameworkCore.PostgreSQL

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.EntityFrameworkCore;


using var context = new BloggingContext();

context.Blogs.Add(new Blog
{
    JustJson = new JustJson
    {
        Name = "SomeJson",
        ChildJustJson = new ChildJustJson()
    },
    Url = "SomeUrl"
});

context.SaveChanges();

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Blog>()
            .OwnsOne(c => c.JustJson, d =>
            {
                d.ToJson();
                d.OwnsOne(x => x.ChildJustJson, builder => builder.ToJson());
            });
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder.UseNpgsql("Host=localhost:5432;Username=postgres;Password=06DKqM9jyLcEtd8n;Database=test");
}

public class Blog
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Key]
    public int BlogId { get; set; }
    public string? Url { get; set; }
    public JustJson? JustJson { get; set; }
    public List<Post> Posts { get; set; } = new List<Post>();
}

public class Post
{
    public int PostId { get; set; }
    public string? Title { get; set; }
    public string? Content { get; set; }

    public int BlogId { get; set; }
    public Blog Blog { get; set; }
}

public class JustJson
{
    public string? Name { get; set; }
    public ChildJustJson? ChildJustJson { get; set; }
}

public class ChildJustJson
{
    public string? Name { get; set; }
}

I tried everything I can think of, from enabling logging, searching on the internet but can't find an answer to the issue.

I hope if anyone can help me in solving this issue.

Best Regards, Asif

Entity Framework Core Training
Entity Framework Core Training
Entity Framework Core: A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.Training: Instruction to develop new skills.
11 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Pradeep M 8,660 Reputation points Microsoft External Staff Moderator
    2025-05-08T08:34:55.4533333+00:00

    Hi Asif A,

    Thank you for reaching out to Microsoft Q & A forum. 

    The ArgumentNullException you're encountering is likely caused by EF Core not properly handling nested JSON owned types without explicitly defined property names. 

    In your OnModelCreating method, specify the JSON column names for the nested objects: 

    modelBuilder.Entity<Blog>()
        .OwnsOne(c => c.JustJson, d =>
        {
            d.ToJson("JustJson");
            d.OwnsOne(x => x.ChildJustJson, b => b.ToJson("ChildJustJson"));
        });
    
    

    Additionally, ensure you are using EF Core and Npgsql 7.0.4 or higher, as later versions provide better support for JSON column mappings.     

    If you have found the answer provided to be helpful, please click on the "Accept answer/Upvote" button so that it is useful for other members in the Microsoft Q&A community.  


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.