Fix nested JSON owned entities causing ArgumentNullException in EF Core 10#347
Merged
roji merged 3 commits intoefcore:mainfrom Jan 22, 2026
Merged
Fix nested JSON owned entities causing ArgumentNullException in EF Core 10#347roji merged 3 commits intoefcore:mainfrom
roji merged 3 commits intoefcore:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request fixes an ArgumentNullException that occurs in EF Core 10 when using naming conventions with JSON-mapped owned entities that have nested owned collections. The issue was caused by incorrectly passing the parent's container column name to nested entities within a JSON structure.
Changes:
- Added logic to detect when an owned entity is nested within a JSON-mapped parent entity
- Modified handling to clear relational annotations for nested JSON entities without setting container column names
- Refactored
ProcessJsonOwnedEntityto remove thecontainerColumnNameparameter, ensuring it's only set on root JSON entities
Comments suppressed due to low confidence (1)
EFCore.NamingConventions/Internal/NameRewritingConvention.cs:210
- There is code duplication between lines 180-186 (nested JSON entity handling) and lines 202-210 (ProcessJsonOwnedEntity). The logic for clearing relational annotations (TableName, Schema, and ColumnName) is repeated. Consider extracting this into a helper method to improve maintainability.
ownedEntityType.Builder.HasNoAnnotation(RelationalAnnotationNames.TableName);
ownedEntityType.Builder.HasNoAnnotation(RelationalAnnotationNames.Schema);
foreach (var property in ownedEntityType.GetProperties())
{
property.Builder.HasNoAnnotation(RelationalAnnotationNames.ColumnName);
}
context.StopProcessing();
return;
}
if (ownedEntityType.GetContainerColumnName() is { } containerColumnName)
{
// Rewrite container column name only on the root JSON entity.
ownedEntityType.SetContainerColumnName(_namingNameRewriter.RewriteName(containerColumnName));
}
ProcessJsonOwnedEntity(ownedEntityType);
void ProcessJsonOwnedEntity(IConventionEntityType entityType)
{
entityType.Builder.HasNoAnnotation(RelationalAnnotationNames.TableName);
entityType.Builder.HasNoAnnotation(RelationalAnnotationNames.Schema);
// TODO: Note that we do not rewrite names of JSON properties (which aren't relational columns).
// TODO: We could introduce an option for doing so, though that's probably not usually what people want when doing JSON
foreach (var property in entityType.GetProperties())
{
property.Builder.HasNoAnnotation(RelationalAnnotationNames.ColumnName);
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Member
|
Thanks @brendonparker! I did a bit of cleanup and added test coverage, will release 10.0.1 with the fix shortly. |
This was referenced Jan 22, 2026
Closed
Closed
Merged
This was referenced Jan 31, 2026
This was referenced Feb 19, 2026
Closed
Closed
Closed
Closed
Closed
Closed
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When using naming conventions with JSON-mapped owned entities that have nested owned collections, an
ArgumentNullExceptionis thrown during query compilation:This occurs when there are relations/children off of a entity marked with
ToJson()For example:
Root Cause
The
ProcessJsonOwnedEntitymethod was passing the parent'scontainerColumnNameto all nested entities and callingSetContainerColumnName()on them. Nested JSON entities shouldn't have their own container column - they're part of the parent's JSON structure, not separate database columns.When nested entities are off of an entity that has
ToJson()called, they weren't being recognized as part of the JSON structure and were incorrectly processed.Fix
Detect when an owned entity is nested within an already JSON-mapped parent via
foreignKey.PrincipalEntityType.IsMappedToJson()Handle nested JSON entities by clearing relational annotations and returning early - they don't need container column names
Only rewrite the container column name on the root JSON entity
Fixes #346